sql分類彙總查詢語句

時間 2021-05-05 20:53:25

1樓:匿名使用者

醉含笑的很牛,不過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

2樓:醉含笑

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

3樓:我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

以上,希望對你有所幫助

4樓:

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

5樓:匿名使用者

如果你用的是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 就得到你要的效果了你要說是在什麼資料庫下,資料庫不同寫法也是有一定差別的

6樓:世界大同喵

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

7樓:匿名使用者

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基礎查詢語句,sql簡單查詢語句

greate table insert selce filetad,name,calss,form student sql簡單查詢語句 1 首先開啟資料庫,建立好表。2 然後查詢全體教師的教師號 姓名 職稱,並分別為三列指定別名 教師號 姓名 職稱,如下圖所示。3 查詢結果如下圖所示。4 接著查詢所...

sql查詢語句 t 是什麼意思,SQL查詢語句 T 是什麼意思

查詢的資料是放在一個虛擬表中的,t是你給這個虛擬表的命名,你還可以命名成其他名稱,這樣做會方便你後續的查詢操作 sql語句中的t.是什麼意思,例如 select from cityinfo t where t sql語句中的t.表示表的別名的字首,可在查詢欄位中引用表的別名。工具 sqlserver...

sql查詢top關鍵字,sql查詢語句 top n的用法

蘇 操作步驟如下 1 首先假設在sql server中有一個基本的資料庫,有6條資料。2 然後我們利用top關鍵字,就能利用top後面跟著數字就能篩選條數。3 此時執行測試,因為top後面的關鍵字為2,所以有2條資料。這樣就完成了操作。結構化查詢語言 structured query languag...