sql 统计用的sql
mh:工時(shí)???mhtype:工時(shí)類型(6種)
字段:userid??????mhtype????mh
??????001??????????1????????5
??????001??????????1????????3
??????001??????????2????????4
??????001??????????3????????9
??????002??????????5????????5
??????002??????????6????????7
??????002??????????3????????4
??????002??????????3????????9
要求統(tǒng)計(jì)出每個(gè)人每一類工時(shí)的總合
結(jié)果要求如下格式
userid??mhtype1???mhtype2???mhtype3???mhtype4???mhtype5???mhtype6???allmh
001?????8?????????4??????????9????????0???????????0?????????0????????21
002?????0?????????0??????????13????????0???????????5?????????7????????25
-------------------
create?table?lk4( userid?mediumint(3)?unsigned?zerofill?not?null, mhtype?int?not?null, mh?int?not?null); insert?into?lk4?values (001,1,5), (001,1,3), (001,2,4), (001,3,9), (002,5,5), (002,6,7), (002,3,4), (002,3,9); select?userid, sum((case?when?mhtype=1?then?mh?else?0?end?))?as?'mhtype1', sum((case?when?mhtype=2?then?mh?else?0?end?))?as?'mhtype2', sum((case?when?mhtype=3?then?mh?else?0?end?))?as?'mhtype3', sum((case?when?mhtype=4?then?mh?else?0?end?))?as?'mhtype4', sum((case?when?mhtype=5?then?mh?else?0?end?))?as?'mhtype5', sum((case?when?mhtype=6?then?mh?else?0?end?))?as?'mhtype6', sum(mh)?as?'allmh' from?lk4?group?by?userid;
結(jié)果:
+--------+---------+---------+---------+---------+---------+---------+-------+
|?userid?|?mhtype1?|?mhtype2?|?mhtype3?|?mhtype4?|?mhtype5?|?mhtype6?|?allmh?|
+--------+---------+---------+---------+---------+---------+---------+-------+
|????001?|???????8?|???????4?|???????9?|???????0?|???????0?|???????0?|????21?|
|????002?|???????0?|???????0?|??????13?|???????0?|???????5?|???????7?|????25?|
+--------+---------+---------+---------+---------+---------+---------+-------+
2?rows?in?set?(0.00?sec)
?
*******************************
數(shù)據(jù)結(jié)構(gòu)如下
?id????name????area
?01????a???????河北
?02????b???????山東
?03????c???????河北??
?04????d???????陜西
?05????e???????河北
?06????f???????山東
?
也即每個(gè)用戶都有id,name,area
?現(xiàn)要求如下:
?按地區(qū)聚合并統(tǒng)計(jì)用戶數(shù)、顯示各用戶詳細(xì)情況
?
大概類似于下面的結(jié)果:
?地區(qū)??總用戶數(shù)???詳細(xì)用戶
?河北???3???????(01,a),(03,c),(05,e)
?山東???2???????(02,b),(06,f)
?陜西???1???????(04,d)
select?area,count(*),group_concat('(',id,',',name,')')
from?tb
group?by?area
?
?
************
?
這是源數(shù)據(jù),根據(jù)不同的adminid,算出每個(gè)adminid有多少條數(shù)據(jù),并且state為0的有幾條,為1的有幾條
返回結(jié)果是這樣的:
adminid?total?state0?state1
???1?????????100???90????????10
??2??????????111???55????????56
total是state的總數(shù),state0是state=0的數(shù)量,state1是state=1的數(shù)量,adminid是adminid
然后將以下得到的數(shù)據(jù)全部插入表2內(nèi):
表2字段為:
adminid?s_total?s_state0?s_state1
insert?into?tb2
select?adminid,count(*)?as?total,
count(case?when?state=0?then?1?end)?as?state0,
count(case?when?state=1?then?1?end)?as?state1
from?tb
group?by?adminid
***********
?一。以產(chǎn)品編碼為分類進(jìn)行數(shù)量的匯總
SQL code?| 1 2 3 | select?產(chǎn)品編碼,sum(購買數(shù)量) from?表結(jié)構(gòu) group?by?產(chǎn)品編碼 |
二。在匯總的基礎(chǔ)上,我要對(duì)每個(gè)產(chǎn)品編碼按發(fā)貨地區(qū)進(jìn)行分類匯總。
| 1 2 3 | select?產(chǎn)品編碼,發(fā)貨地區(qū),sum(購買數(shù)量) from?表結(jié)構(gòu) group?by?產(chǎn)品編碼,發(fā)貨地區(qū) |
總結(jié)
以上是生活随笔為你收集整理的sql 统计用的sql的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA的23种设计模式
- 下一篇: DRBD+HeartBeat+NFS 架