sql按分類級別彙總,sql 按分類級別彙總

時間 2021-10-16 11:00:39

1樓:匿名使用者

按分類級別的話,用group by進行分組,如果彙總的話,用sum求和,用count計數。

sql:select count(1),sum(sorce) from tablename group by class_no;

解釋:以上是假設等級欄位為class_no,之後求出每個等級的總條數和對應的「sorce」和。

2樓:斯內科

很明顯 a,b,c,d都是兩位字串遞增

--一級:

select a. *,b.分類名稱 from 商品表 a,分類表 b where charindex(b.分類編碼,a.分類編碼,1)=1

and b.分類等級=1

--二級:

select a. *,b.分類名稱 from 商品表 a,分類表 b where charindex(b.分類編碼,a.分類編碼,1)=1

and b.分類等級=2

--**、四級只需改等級=3,4即可

3樓:傳說中的鷹王

分類表a 商品表b

2級查詢

select left(a.分類編碼,4),b.分類名稱,商品編碼,商品名稱 from b inner join

a on left(a.分類編碼,4)=b.分類編碼3級查詢

select left(a.分類編碼,6),b.分類名稱,商品編碼,商品名稱 from b inner join

a on left(a.分類編碼,6)=b.分類編碼

sql 分類彙總查詢語句

4樓:匿名使用者

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

5樓:醉含笑

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

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

以上,希望對你有所幫助

7樓:

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

8樓:匿名使用者

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

9樓:世界大同喵

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

10樓:匿名使用者

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按列分組求和

你的日期欄位是字串型的?如果是可以這樣 select left 日期,case substring 日期,7,1 when then 6 else 7 end as 日期,貨號,sum 數量 as 數量,sum 金額 as 金額 from table1 group by left 日期,case s...

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

select 商品id sum 商品數量 as total from 表名 group by 商品id order by total desc select 商品id sum 商品數量 as 商品數量from u table group by 商品id order by 商品id,sum 商品數量 ...

按高爾夫水平分類有哪些級別

景愛呀 通俗的區分有以下幾類 1 初學者。剛剛接觸,對揮杆技術要領初步體會但還不能準確的重複,下場成績在120杆以上。2 業餘球員。接觸在1 2年左右甚至更長,對揮杆技術要領有一定的認知,但準確性時有起伏,下場成績一般在100杆以上或左右 俗稱三輪車級別 其中細分可以分為9字頭球員 平均90杆 99...