我有一个名为Customer的表,具有以下架构。
Create Table Customer(id Number,customer_type varchar(20),customer_status char(1),account_number varchar(20));
Insert into Customer(id,customer_type,customer_status,account_number)values(123,'RETAIL','A','32456798');
Insert into Customer(id,customer_type,customer_status,account_number)values(123,'RETAIL','I','92456798');
Insert into Customer(id,customer_type,customer_status,account_number)values(123,'RETAIL','P','22456798');
Insert into Customer(id,customer_type,customer_status,account_number)values(123,'PERSONAL','A','42456798');
Insert into Customer(id,customer_type,customer_status,account_number)values(123,'PERSONAL','I','52456798');
Insert into Customer(id,customer_type,customer_status,account_number)values(123,'PERSONAL','P','62456798');
commit;
我正在尝试获取客户状态为活跃的ID。Customer_type可以为两种类型:[[RETAIL或PERSONAL。如果ID具有任何有效的帐户,则我只想返回Retail true,否则为false,与个人相同我在下面的查询中尝试过,但是在返回ID时遇到问题
select REATIL,PERSONAL from (select case when customer_status = 'A' then 'Y' else 'N' end as REATIL from Customer where customer_status='A' and customer_type='RETAIL')
,(select id, case when customer_status = 'A' then 'Y' else 'N' end as PERSONAL from Customer where customer_status='A' and customer_type='PERSONAL');
预期输出:
可以提供帮助。
select C.id, R.account_number, P.account_number from Customer C
left join Customer R on R.id = c.id
left join Customer P on P.id = c.id
where R.customer_type = 'RETAIL'
and R.customer_status = 'A'
and P.customer_type = 'PERSONAL'
and P.customer_status = 'A'
select ID,
Case When customer_type = 'RETAIL' and customer_status = 'A' then 'Y'
When customer_type = 'RETAIL' and customer_status != 'A' then 'N'
Else ''
End as Retail,
Case When customer_type = 'PERSONAL' and customer_status = 'A' then 'Y'
When customer_type = 'PERSONAL' and customer_status != 'A' then 'N'
Else ''
End as PERSONAL,
account_number
from Customer