關於SQL分組統計

來源:互聯網
上載者:User
關於SQL分組統計

我想做一個分組統計,按人員分組 欄位名為pname, 然後統計每個提出過多少問題,已經解決多少個問題,未解

決多少個問題, 表中4個欄位 ID,pname,question,decide 第一個欄位是ID,第二個欄位是人名,第三個欄位是

這個人提的問題,第4個欄位是問題是否解決是個標誌欄位,"y"就是解決了 "n"就是沒解決 ,現在我想統計每個

人都提了多少問題,其中解決多少,沒解決多少? 請大蝦門 幫幫忙.

使用case

select pname,count(*) as 提問數量,sum(case decide when 'y' then 1 else 0 end) as 已解決數量,sum

(case decide when 'n' then 1 else 0 end) as 沒解決數量
from 表
group by pname

isnull可以將null值替換

下面的樣本為 titles 表中的所有書選擇書名、類型及價格。如果一個書名的價格是 NULL,那麼在結果集中

顯示的價格為 0.00。
USE pubs
GO
SELECT SUBSTRING(title, 1, 15) AS Title, type AS Type,
ISNULL(price, 0.00) AS Price
FROM titles
GO
下面是結果集:
Title Type Price
--------------- ------------ --------------------------
The Busy Execut business 19.99
Cooking with Co business 11.95
You Can Combat business 2.99
Straight Talk A business 19.99
Silicon Valley mod_cook 19.99
The Gourmet Mic mod_cook 2.99
The Psychology UNDECIDED

Count 函數不統計包含 Null 欄位的記錄,除非 expr 是星號 (*) 萬用字元。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.