分類統計相關的sql語句怎麼寫,sql 分類彙總查詢語句

時間 2021-05-07 20:01:56

1樓:維也納的孩童戶

以下語句可用於ms sql server。

select count(1) as 數量, c.使用者組名稱 from 檔案表 a inner join 使用者表 b on a.釋出人 = b.

使用者名稱 inner join 使用者組表 c on b.使用者組id = c.id group by c.

id, c.使用者組名稱 order by 數量

sql 分類彙總查詢語句

2樓:匿名使用者

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

3樓:醉含笑

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

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

以上,希望對你有所幫助

5樓:

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

6樓:匿名使用者

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

7樓:世界大同喵

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

8樓:匿名使用者

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

oracle資料分類統計的sql語句怎麼寫

9樓:匿名使用者

select a.number 號碼,sum(case when b.分類 = '祝福' then 1 else 0 end) 祝福,

sum(case when b.分類 = '養生' then 1 else 0 end) 養生,

sum(case when b.分類 = '幽默' then 1 else 0 end) 幽默

from a,b

where a.content = b.conentgroup by a.number

10樓:★企鵝寶寶

好難寫啊,感覺如果要實現你這個效果,可能得改一下表結構吧,反正我只能寫成這樣,你看能可以麼。。。

select distinct number,分類,count(a.content) from(select number,a.content,分類 from a,b where a.

content=b.content) group by number,分類 order by number,分類;

查出來效果是這樣的

123 養生 1

123 祝福 1

213 幽默 2

213 祝福 1

求sql分類計數語句

11樓:匿名使用者

select distinct 姓名,專案,count(專案)  from 表名 group by 姓名,專案

12樓:白菜一顆

select 姓名,專案,count(時間) from 表 group by 姓名,專案

這樣就可以了

sql語句如何統計種類數

13樓:匿名使用者

要得到每組的合計可以用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 如何寫語句 按客戶分類,統計每年的所有金額!麻煩幫忙寫一下,急!**等

14樓:匿名使用者

不知道您是哪個資料庫,sqlserver可以如此寫

select 客戶名稱,

sum(case left(交易日期,4) when '2010' then 金額 else 0 end) "2023年",

sum(case left(交易日期,4) when '2011' then 金額 else 0 end) "2023年",

sum(case left(交易日期,4) when '2012' then 金額 else 0 end) "2023年",

sum(case left(交易日期,4) when '2013' then 金額 else 0 end) "2023年"

from [table]

group by 客戶名稱

統計報表上報制度,統計報表的相關制度

巴掌說法 1 本制度所稱統計報表包括月報表 季報表 年報快報等。2 統計報表報送工作堅持依法 依紀的原則,堅持客觀性 真實性 時間性 一致性的原則。3 統計報表報送工作由各基層單位統計人員負責,人員必須具備一定業務能力和綜合素質。要保持統計人員的相對穩定。4 統計工作人員要認真學習統計指標解釋,確定...

腫瘤相關抗原的種類,腫瘤抗原的定義和分類?

因為愛所以窮 腫瘤等級的分級因腫瘤型別而異。一般來說,根據異常的程度,腫瘤被分為1 2 3或4級。在一級腫瘤中,腫瘤細胞和腫瘤組織的組織看起來接近正常。這些腫瘤往往生長和擴散緩慢。相反,3級和4級腫瘤的細胞和組織看起來不像正常的細胞和組織。3級和4級腫瘤的生長速度和擴散速度比低階別腫瘤快。建議到正規...

如何在EXCEL中進行資料分類統計並用巨集統計結果可生成新表

建議使用透視表吧,而且不同的分類統計可以放到不同的工作表中。研究一下資料透視表,應該可以滿足你需求。 有規則vba可以完成。 需要依據你的實際檔案來寫 這種情況 你需要給一個 出來啊.什麼樣的 要統計成什麼樣的結果? 最好的方法,手工做,做的時候錄製巨集,這樣你做了一次,以後處理同樣的 的時候就可以...