生活随笔
收集整理的這篇文章主要介紹了
MySQL——单表查询练习:彩票数据核对
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
單表查詢練習(xí):彩票數(shù)據(jù)核對(duì)練習(xí)
彩票游戲規(guī)則:
彩票有10個(gè)刮獎(jiǎng)區(qū),每個(gè)刮獎(jiǎng)區(qū)有一個(gè)圖符和一個(gè)獎(jiǎng)符,彩票中獎(jiǎng)金額 = 同一個(gè)刮獎(jiǎng)區(qū)內(nèi)的(圖符倍數(shù)* 獎(jiǎng)符金額),同時(shí)每張彩票售價(jià)5元
所有彩票數(shù)據(jù)存儲(chǔ)在lottery.csv文件中,共計(jì)600000條記錄。根據(jù)此數(shù)據(jù)集完成以下練習(xí):
求總中獎(jiǎng)張數(shù)及金額求各不同獎(jiǎng)符的張數(shù)及金額(獎(jiǎng)符為5元、10元等)求中獎(jiǎng)張數(shù)與總張數(shù)占比檢查每個(gè)本號(hào)中有100張彩票檢查每本彩票中最多只有一張中獎(jiǎng)彩票金額超過50元檢查每本彩票中最多只有連續(xù)7張無獎(jiǎng)票
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導(dǎo)入'''
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 中獎(jiǎng)總張數(shù)
,sum(bingovalue
) as 中獎(jiǎng)總金額
from lottery
where bingovalue
!= 0;
select bingovalue
as 獎(jiǎng)符
,count(bingovalue
) as 張數(shù)
,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 中獎(jiǎng)張數(shù)占比
, sum(bingovalue
)/@allsum as 中獎(jiǎng)金額占比
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;
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的MySQL——单表查询练习:彩票数据核对的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。