1

このクエリを実行すると:

select
    '1992' = CASE WHEN Year(HireDate) = 1992 THEN count(*) END,
    '1993' = CASE WHEN Year(HireDate) = 1993 THEN count(*) END,
    '1994' = CASE WHEN Year(HireDate) = 1994 THEN count(*) END
  from employees
 group by Year(HireDate)

私は得る:

1992     1993      1994
3        NULL      NULL
NULL      3        NULL
NULL     NULL       3

クエリを変更して次の情報を取得するにはどうすればよいですか。

1992    1993    1994
3           3      3
4

2 に答える 2

3

このクエリでは、行のみが取得されます。

select 
'1992'=
count( CASE WHEN Year(HireDate) = 1992 THEN 1 END ),
'1993' =
count( CASE WHEN Year(HireDate) = 1993 THEN 1 END ),
'1994' = 
count( CASE WHEN Year(HireDate) = 1994 THEN 1 END )
from employees
于 2012-09-26T07:24:23.903 に答える
1
select 
  '1992' = sum (case when year(HireDate) = 1992 THEN 1 else 0 END),
  '1993' = sum (case when year(HireDate) = 1993 THEN 1 else 0 END),
  '1994' = sum (case when year(HireDate) = 1994 THEN 1 else 0 END),
from employees
于 2012-09-26T07:24:25.267 に答える