如果我有下面的示例數據:SQL GROUP BY與濾波器SELECT
╔══════════════╦══════════════════╦════════════╦═══════╗
║ Client ║ con_id ║ mat1_07_03 ║ Ccode ║
╠══════════════╬══════════════════╬════════════╬═══════╣
║ Clients Name ║ C13109BBFD511534 ║ $1,062.00 ║ NOFL ║
║ Clients Name ║ C11AC9BBF74D6882 ║ $879.73 ║ NOFL ║
║ Clients Name ║ C12A69BBF1ACB578 ║ $2,790.29 ║ NOFA ║
║ Clients Name ║ C12A69BBF1ACB578 ║ $912.00 ║ NOFL ║
║ Clients Name ║ C6B0CA1A767C9744 ║ $2,180.11 ║ NOFL ║
║ Clients Name ║ C11AC9BBF74D6882 ║ $878.67 ║ NOFA ║
║ Clients Name ║ C13B79BBF4F1F450 ║ $300.00 ║ NOFL ║
║ Clients Name ║ C12A69BBF1ACB578 ║ $1,790.67 ║ NOFL ║
║ Clients Name ║ CA6869E2FE38A449 ║ $240.00 ║ NOFA ║
║ Clients Name ║ C46439FB0D847140 ║ $3,392.66 ║ NOFL ║
║ Clients Name ║ C12A69BBF1ACB578 ║ $1,791.73 ║ NOFA ║
║ Clients Name ║ C13B49BBF12ED236 ║ $0.00 ║ NOFL ║
║ Clients Name ║ C12A69BBF1ACB578 ║ $879.73 ║ NOFL ║
╚══════════════╩══════════════════╩════════════╩═══════╝
,並應用以下查詢:
SELECT
[Client]=MAX(m.Client)
,[CaseCount]=COUNT(m.con_id)
,[AmtInDispute]=CONVERT(char, SUM(Convert(money, m.mat1_07_03)), 101)
FROM lntmu11.matter m
GROUP BY m.con_id
ORDER BY COUNT(m.Client) DESC
我怎樣才能進一步羣Ccode
列,以獲得COUNT
有多少行是 NOFA
或NOFL
我所需的輸出將顯示爲:
╔══════════╦═══════════╦═══════════════╦═══════════════╦══════════════╗
║ Client ║ CaseCount ║ NOFACaseTotal ║ NOFLCaseTotal ║ AmtInDispute ║
╠══════════╬═══════════╬═══════════════╬═══════════════╬══════════════╣
║ Client A ║ 3548 ║ 2000 ║ 1548 ║ 5,658,307.60 ║
║ Client B ║ 3366 ║ 100 ║ 3266 ║ 2,885,649.48 ║
║ Client C ║ 3014 ║ 800 ║ 2214 ║ 2,851,507.13 ║
║ Client D ║ 2340 ║ 340 ║ 2000 ║ 3,467,207.12 ║
╚══════════╩═══════════╩═══════════════╩═══════════════╩══════════════╝
+1爲了花時間格式化您的問題,所以很好。 – stakx 2014-10-01 19:46:24