0
SELECT *,
    CASE 
    when PERSONNUM in ('x','y','z')
    then 'Home'
    end as HomeEmployees
    when PERSONNUM in ('a','b','c')
    then 'Away'
    end as AwayEmployees
FROM dbo.ALLTOTALS
where PERSONNUM in ('a','b','c','x','y','z')
    and HomeEmployees is not null

-- 選択した PERSONNUM フィールドに基づく複数の場合

4

3 に答える 3

0

ケースをエイリアスに射影し、次のように外側のクエリでフィルタリングすることもできます。

SELECT * -- Or better still, explicitly identify each needed column
FROM
(
  SELECT *, -- Or better still, explicitly identify each needed column
    CASE 
        WHEN PERSONNUM in ('x','y','z')
        THEN 'Home'
        END AS HomeEmployees
    FROM AllTotals
) AS FilteredEmployees 
WHERE 
  FilteredEmployees.HomeEmployees IS NOT NULL;

またはCTEとして:

WITH FilteredEmployees AS
(
  SELECT *, 
    CASE 
        WHEN PERSONNUM in ('x','y','z')
        THEN 'Home'
        END AS HomeEmployees
    FROM AllTotals
)
SELECT *
  FROM FilteredEmployees
  WHERE FilteredEmployees.HomeEmployees IS NOT NULL;
于 2013-09-25T19:46:49.387 に答える