生活随笔
收集整理的這篇文章主要介紹了
MySQL——单表查询练习:彩票数据核对
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
單表查詢練習:彩票數據核對練習
彩票游戲規則:
彩票有10個刮獎區,每個刮獎區有一個圖符和一個獎符,彩票中獎金額 = 同一個刮獎區內的(圖符倍數* 獎符金額),同時每張彩票售價5元
所有彩票數據存儲在lottery.csv文件中,共計600000條記錄。根據此數據集完成以下練習:
求總中獎張數及金額求各不同獎符的張數及金額(獎符為5元、10元等)求中獎張數與總張數占比檢查每個本號中有100張彩票檢查每本彩票中最多只有一張中獎彩票金額超過50元檢查每本彩票中最多只有連續7張無獎票
use test
;
create table lottery123
( FNo
varchar(10) not null,
TNo
varchar(10) not null,
Mark
varchar(20) not null,
reward
varchar(20) not null,
bingovalue
int not null );'''還是決定用navicat導入'''
select * from lottery
limit 10;
use test
;
alter table lottery
add id
int;
alter table lottery
modify id
int not null auto_increment primary key first;
select count(bingovalue
) as 中獎總張數
,sum(bingovalue
) as 中獎總金額
from lottery
where bingovalue
!= 0;
select bingovalue
as 獎符
,count(bingovalue
) as 張數
,sum(bingovalue
) as 金額
from lottery
where bingovalue
!= 0
group by bingovalue
;
set @allcount=(select count(*) from lottery
);
set @allsum=(select count(*)*5 from lottery
);
select count(bingovalue
)/@allcount as 中獎張數占比
, sum(bingovalue
)/@allsum as 中獎金額占比
from lottery
where bingovalue
!= 0;
select FNo
, count(FNo
) from lottery
group by FNo
having count(FNo
) != 100;
select FNo
,count(FNo
) from lottery
where bingovalue
>50
group by FNo
having count(FNo
)>1;
select *,rownumber1
-rownumber
-1 as gap
from(select *, lead
(rownumber
,1) over(partition by Fno
) as rownumber1
from(select * from lottery
where bingovalue
<>0) as a
) as b
where rownumber1
-rownumber
-1>=7;
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的MySQL——单表查询练习:彩票数据核对的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。