sql語句分類彙總,排序查詢語句怎麼實現

時間 2021-10-16 10:57:38

1樓:匿名使用者

select 商品id , sum(商品數量) as total from 表名 group by 商品id order by total desc

2樓:匿名使用者

select 商品id ,sum(商品數量) as 商品數量from u_table

group by 商品id

order by 商品id,sum(商品數量)--以上為彙總

select 商品id , 商品數量

from u_table

group by 商品id

order by 商品id, 商品數量

3樓:dotnetcore專題

select 商品id from 表或檢視名

group by 商品id

order by sum(商品數量) desc

4樓:匿名使用者

select a.id,sum(a.cnt) cntfrom table a

group by a.id

order by cnt,a.id desc不過彙總後商品id變為唯一了,先按數量排序這個沒問題,但再按商品id順序從大到小排序這個意義就不大了。

5樓:匿名使用者

select 商品id,sum(商品數量) 商品數量 from 表名 group by 商品id order by sum(商品數量) desc

這樣試試

sql語句如何對select的結果進行分類統計(或者說如何用檢視實現統計查詢) 20

6樓:折柳成萌

直接下面一句話就可以了:

select 單位名稱,count(case 專案類別 when '理工類' then 1 end) 理工類,

count(case 專案類別 when '社科類' then 1 end) 社科類,

count(case 專案性質 when '橫向' then 1 end) 橫向,

count(case 專案性質 when '縱向' then 1 end) 縱向,

count(case 專案性質 when '校外' then 1 end) 校外,

count(*) 總數

from item_info

groupo by 單位名稱

sql 分類彙總查詢語句

7樓:匿名使用者

醉含笑的很牛,不過sum(pay)有點需要改動最終完美版:

select min(id) as 序號,max(dept) as 部門,

sum(case when zt='01' or zt='02' then pay else 0 end) as 合計,

sum(case zt when 01 then 1 else 0 end) as 個數01狀態,

sum(case zt when 02 then 1 else 0 end) as 個數02狀態,

count(zt) as 總數

from aac

group by dept

8樓:醉含笑

select min(id) as 序號,

max(dept) as 部門,

sum(pay) as 合計,

sum(case zt when '01' then 1 else 0 end) as 個數01狀態,

sum(case zt when '02' then 1 else 0 end) as 個數02狀態,

count(zt) as 總數

from 表名

group by dept

這段**是sqlserver和oracel通用,其中「表名」的地方,換成你的表名

喔看掉了這個條件:我現在想統計01、02兩種狀態的數量和pay合計

還是 zjwssg提醒,但最後兩個sum中when後面,建議還是加單引號吧

把上面的**改為這樣應該沒問題了

select min(id) as 序號,

max(dept) as 部門,

sum(case when zt='01' or zt='02' then pay else 0 end) as 合計,

sum(case zt when '01' then 1 else 0 end) as 個數01狀態,

sum(case zt when '02' then 1 else 0 end) as 個數02狀態,

count(zt) as 總數

from 表名

group by dept

9樓:我tm不管

select dept as 部門,sum(pay) as 合計,sum(case zt when '01'then 1 else 0 end) as 個數(01狀態),

sum(case zt when '02'then 1 else 0 end) as 個數(02狀態),count(*) as 總數

from 表 group by dept

以上,希望對你有所幫助

10樓:

select

row_number() over(order by a.dept) 序號,

a.dept 部門,

a.合計,

b.個數01,

c.個數02,

d.總數

from

(select dept,sum(pay) 合計 from t where zt='01' or zt='02' group by dept) a,

(select dept,count(pay) 個數01 from t where zt='01' group by dept) b,

(select dept,count(pay) 個數02 from t where zt='02' group by dept) c,

(select dept,count(pay) 總數 from t group by dept) d

where a.dept=b.dept and b.dept=c.dept and c.dept=d.dept

參照樓上的寫法,改進一下有:

select

row_number() over(order by dept) 序號,

dept as 部門,

sum(case when zt='01' or zt='02' then pay else 0 end) 合計,

sum(case when zt='01' then 1 else 0 end) as 個數01狀態,

sum(case when zt='02' then 1 else 0 end) as 個數02狀態,

count(*) as 總數

from t

group by dept

11樓:匿名使用者

如果你用的是sql server可以:

select 序號=identity(int,1,1),dept as 部門,sun(pay) as 合計,sum(case when zt='01' then 1 else 0 end) as 個數01狀態,sum(case when zt='02' then 1 else 0 end) as 個數02狀態,count(*) as 總數 into #tmp_total from yourtablename group by dept

select * from #tmp_total 就得到你要的效果了你要說是在什麼資料庫下,資料庫不同寫法也是有一定差別的

12樓:世界大同喵

create table tb (id int,dept varchar(10),pay int,zt int)

insert tb select 1,'辦公室',20,1

union all select 2,'局領導',10,2

union all select 3,'辦公室',40,3

union all select 4,'局領導',10,1

union all select 5,'辦公室',50,1

union all select 6,'局領導',10,2

union all select 7,'辦公室',20,2

union all select 8,'局領導',10,2

select identity(int,1,1) as 序號,

dept as 部門,

sum(case when zt='01' or zt='02' then pay else 0 end) 合計,

sum(case when zt='1' then 1 else 0 end) 個數01狀態,

sum(case when zt='2' then 1 else 0 end) 個數02狀態,

count(*) as 總數 into #temp from tb group by dept

select * from #temp

13樓:匿名使用者

select a.dept,a.pay,c.[01],c.[02],b.ztnum

from

(select dept,sum(pay) as pay from table_1 where zt in(01,02) group by dept

)aleft join

(select dept,count(zt) as ztnum from table_1 group by dept

) bon a.dept=b.dept

left join

(select *

from

(select dept,zt,count(zt) as ztnum from table_1 where zt in(01,02) group by dept,zt)a

pivot

(sum(a.ztnum)

for a.zt in ([01],[02])

) as tpivot

) con b.dept=c.dept

sql語句 如何分組後得到記錄總數

14樓:大野瘦子

select count(*)

from (select 分組欄位 from 表group by 分組欄位

)別名或者 select count(*)

from (select distinct 分組欄位 from 表)別名

15樓:突擊手覓患

要得到每組的合計可以用2、3樓,要總計用1樓,想一次性得到分組合計以及總計,sql2005可以這麼寫:

select 分組欄位 from 表

group by 分組欄位

compute sum(count(*))*****

那就這樣

select count(*)

from (select 分組欄位 from 表group by 分組欄位

)別名或者

select count(*)

from (select distinct 分組欄位 from 表)別名

sql語句查詢,sql語句查詢

表要告訴我們呀,不要還要我們設計資料庫吧? 給表啊 我想查查這個玩玩 1.select from student 2.select id,name,age from student 我有例題你要嗎 靠 這麼多東西幫你寫不曉得要死多少腦細胞 分還是少了點 這點分。sql語句查詢不等於怎麼查不出來? 大...

sql查詢語句 多重查詢,SQL查詢語句,怎樣查詢重複資料

select count num,systemfrom site visitmessagewhere visit time 2009 07 17 03 20 22 and visit time 2009 07 27 03 20 22 order by num desc select system,c...

模糊查詢Sql語句問題,SQL模糊查詢語句怎麼寫啊

理工愛好者 模糊之後估計效率不太好 如果知道z是開頭字母 select name where pyname like z s 如果zs都是中間字母 select name where pyname like z s 祝好運,望採納。 select from 表面 like zs like是不分大小寫...