我有以下CASE查询,如果找到任何内容则返回1,否则返回0。我想将这些转换为单个查询,如果其中任何一个为真,则返回1,否则返回0。我该怎样才能完成这个?
SELECT CASE WHEN count(PLACE.CODE) > 0 THEN 1 ELSE 0 END
FROM PLACE
WHERE STYLE = 'RED'
AND RULES = 'NO'
SELECT CASE WHEN count(GARDEN.AREA) > 0 THEN 1 ELSE 0 END
FROM GARDEN
WHERE PLACE = 'GROUND'
AND MAZE = '1'
SELECT CASE WHEN count(place_area.AVAILABLE_AREA) > 0 THEN 1 ELSE 0 END
FROM PLACE_AREA as place_area
INNER JOIN USED_PLACE as used_place
ON used_place.COLOR = 'RED'
AND used_place.MAKE = 'INDUSTRY'
WHERE place_area.CODE = 'FLOOR'
AND place_area.DANCE = '0'
答案 0 :(得分:1)
如果我理解正确,您可以union all
结果并计算sum
,如果sum > 0
则至少有一个1
Select Case When Sum(x.col) > 0 THEN 1 ELSE 0 END from
(
SELECT CASE WHEN count(PLACE.CODE) > 0 THEN 1 ELSE 0 END as col
FROM PLACE
WHERE STYLE = 'RED'
AND RULES = 'NO'
Union All
SELECT CASE WHEN count(GARDEN.AREA) > 0 THEN 1 ELSE 0 END as col
FROM GARDEN
WHERE PLACE = 'GROUND'
AND MAZE = '1'
Union All
SELECT CASE WHEN count(place_area.AVAILABLE_AREA) > 0 THEN 1 ELSE 0 END as col
FROM PLACE_AREA as place_area
INNER JOIN USED_PLACE as used_place
ON used_place.COLOR = 'RED'
AND used_place.MAKE = 'INDUSTRY'
WHERE place_area.CODE = 'FLOOR'
AND place_area.DANCE = '0'
) x
答案 1 :(得分:1)
SELECT
CASE
WHEN
EXISTS(
SELECT TOP 1 1
FROM PLACE
WHERE STYLE = 'RED'
AND RULES = 'NO'
AND count(PLACE.CODE) > 0
)
OR EXISTS (
SELECT TOP 1 1
FROM GARDEN
WHERE PLACE = 'GROUND'
AND MAZE = '1'
AND count(GARDEN.AREA) > 0
)
OR EXISTS (
SELECT TOP 1 1
FROM PLACE_AREA as place_area
INNER JOIN USED_PLACE as used_place
ON used_place.COLOR = 'RED'
AND used_place.MAKE = 'INDUSTRY'
WHERE place_area.CODE = 'FLOOR'
AND place_area.DANCE = '0'
AND count(place_area.AVAILABLE_AREA) > 0
)
THEN 1
ELSE 0
END