1

我在下面的任务中使用 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

请建议。任何帮助都会很棒。

4

1 回答 1

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
;
于 2013-07-16T09:18:19.277 回答