我正在尝试做一些事情(当然是假的):
SELECT
city,
CASE WHEN COUNT( [group] ) > 1 THEN 'Multiple' ELSE [group] END AS Type
FROM
offices
GROUP BY
city
办公室包含以下行:
ID | group | city
----------------------
1 | 'A' | 'Houston'
2 | 'B' | 'Houston'
3 | 'C' | 'Houston'
4 | 'S' | 'Boston'
5 | 'R' | 'Detroit'
结果看起来像是:
city | group
--------------------
'Houston'| 'Multiple'
'Boston' | 'S'
'Detroit'| 'R'
我知道你可以这样做:
SELECT
City,
CASE WHEN COUNT([group]) > 1 THEN
'Multiple'
ELSE
( SELECT [group] FROM test WHERE t.City = city )
END AS CGroup
FROM
test t
GROUP BY
City
我认为这应该更简单 没有子查询的东西?
答案 0 :(得分:8)
您可以找到该列的MIN
和MAX
,然后在它们不相同时执行操作:
declare @t table (ID int not null,[group] char(1) not null,city varchar(20) not null)
insert into @t(ID,[group],city) values
(1,'A','Houston'),
(2,'B','Houston'),
(3,'C','Houston'),
(4,'S','Boston' ),
(5,'R','Detroit')
select city,
CASE
WHEN MIN([group]) != MAX([group]) THEN 'Multiple'
ELSE MAX([group]) END as [group]
from @t
group by city
尽管服务器在MAX
子句中出现两次,但服务器应足够智能,只能实际运行select
聚合一次。
结果:
city group
-------------------- --------
Boston S
Detroit R
Houston Multiple
答案 1 :(得分:2)
@Damien_The_Unbeliever's answer很完美。这个是另一种选择。如果您想检查多个(例如COUNT(GROUP) > 2
)。只需在MIN
中使用MAX
或ELSE
,就像这样:
SELECT
city,
CASE WHEN COUNT([group]) > 2
THEN 'Multiple'
ELSE MAX([group]) END AS Type
FROM
offices
GROUP BY
city
答案 2 :(得分:0)
使用您的第一个查询,但只需使用MIN
部分中的ELSE
SELECT
City,
CASE
WHEN COUNT(*) > 1 THEN 'Multiple'
ELSE MIN(CityGroup)
END AS CityGroup
FROM Test
GROUP BY CityGroup