Sql查询修改

问题描述 投票:-1回答:2

我有一个名为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');

预期输出:

enter image description here

可以提供帮助。

sql oracle oracle11g oracle10g
2个回答
0
投票
尝试使用联接。以下查询返回帐号而不是'Y'

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'


0
投票
尝试以下操作(根据您的进一步要求进行修改)

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

© www.soinside.com 2019 - 2024. All rights reserved.