我的数据表如下所示:
client Status 1 a 2 b 3 a 4 d 5 d 6 a 7 e 8 b 9 e 10 f
假设我想按状态分组,但我希望群组状态a,b,d
(状态I
)为一个群组e,f
(状态II
)作为另一组
我希望它最终看起来像
status Count I 7 II 3
我该如何撰写查询?
答案 0 :(得分:2)
您可以使用CASE语句。
if (selector && selector !== '#') {
$parent = $(selector)
}
答案 1 :(得分:2)
您可以运行分组并计入一个语句。
SELECT
CASE WHEN Status IN ('a', 'b', 'd') THEN 'I' ELSE 'II' END AS Status,
count(*) as count
FROM Table1
GROUP BY (CASE WHEN Status IN ('a', 'b', 'd') THEN 'I' ELSE 'II' END)
ORDER BY Status
Result:
Status count
I 7
II 3
答案 2 :(得分:1)
你可以使用group by case
declare @table table ( client INT, STATUS Varchar(10))
insert into @table
select 1 , 'a' union all
select 2 , 'b' union all
select 3 , 'a' union all
select 4 , 'd' union all
select 5 , 'd' union all
select 6 , 'a' union all
select 7 , 'e' union all
select 8 , 'b' union all
select 9 , 'e' union all
select 10 , 'f'
查询:
select clientgroup, count(*) Count from
(
select case when status in ( 'a','b','d') then 'I' else 'II' end clientgroup from @table
) x group by clientgroup