我在下面的任务中使用Oracle数据库。
我有一张表格,数据如下 -
ID SSN
-------------
10 A
20 A
10 B
20 B
30 B
20 C
现在,我想以下面的格式显示这些记录 -
SSN ID_10_Indicator ID_20_Indicator ID_30_Indicator
----------------------------------------------------------------
A Y Y N
B Y Y Y
C N Y N
我正在使用以下查询 -
select ssn,
(case when ID = '10' then 'Y' else 'N') as ID_10_Indicator,
(case when ID = '20' then 'Y' else 'N') as ID_20_Indicator,
(case when ID = '30' then 'Y' else 'N') as ID_30_Indicator
from table1
group by ssn,
(case when ID = '10' then 'Y' else 'N'),
(case when ID = '20' then 'Y' else 'N'),
(case when ID = '30' then 'Y' else 'N')
但我没有获得SSN的唯一行。相反,我得到的记录如下 -
SSN ID_10_Indicator ID_20_Indicator ID_30_Indicator
----------------------------------------------------------------
A Y N N
A N Y N
B Y N N
B N Y N
B N N Y
C N Y N
请建议。任何帮助都会很棒。
答案 0 :(得分:3)
select ssn,
NVL (MAX (CASE ID when '10' then 'Y' else null end ),'N') as ID_10_Indicator,
NVL (MAX (CASE ID when '20' then 'Y' else null end ),'N') as ID_20_Indicator,
NVL (MAX (CASE ID when '30' then 'Y' else null end ),'N') as ID_30_Indicator
from table1
group by ssn
;