2017-12-04 16 views
0

何かを見つけたら1を返し、それ以外の場合は0を返す次のCASEクエリがあります。私はそれらのいずれかが真である場合は1を返し、そうでない場合は0を返す単一のクエリに変換したいと思います。これをどのように遂行できますか?これらの複数のCASEクエリを1つのクエリに変換するTSQL

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' 

答えて

1

私が正しくあなたを理解していれば、あなたは結果をunion allことができ、sum > 0は、その後、少なくとも1 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 
+0

があった場合、sumの計算は、私が '、 '無効な列名を取得していますcol'' – zero01alpha

+0

@ zero01alpha – RezaRahmati

+0

を編集してもう一度試してみてください。あなたのCase-Whenステートメント – RezaRahmati

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 
関連する問題