Sql查询修改
Sql Query Modification
我有一个名为 Customer 的 table,具有以下架构。
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');
Insert into Customer(id,customer_type,customer_status,account_number)values(243,'PERSONAL','A','02456798');
commit;
我正在尝试获取客户状态为 active.Customer_type 的 Id 可以有两种类型 RETAIL 或 PERSONAL。我只是want return Retail 如果 id 有任何活跃的零售账户,则为真,否则为假,
与 个人 相同
我尝试了以下查询,但我在 returning 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');
预期输出:
|---------------------|------------------|----------------|
| id | Retail | Personal |
|---------------------|------------------|----------------|
| 123 | Y | Y |
|---------------------|------------------|----------------|
| 243 | N | Y |
|---------------------|------------------|----------------|
如果能提供帮助,我们将不胜感激。
尝试使用连接。
以下查询 returns 帐号而不是 'Y'
select C.id, max(R.account_number), max(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'
group by C.id
尝试以下操作(根据您的进一步要求进行修改)。我根据给定的数据执行此操作,但我觉得数据不够广泛,无法测试代码。我做了一些假设,当没有匹配时就把空白。
基本上为了确定 Retail 是 Y 还是 N,我使用了 case 语句并对 Personal 做了同样的事情
-- 该解决方案不适合,因为 OP 需要每个 ID 1 条记录
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
这是所需的解决方案
Select ID, Max(RETAIL) as RETAIL, Max(PERSONAL) as PERSONAL
from
(
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
from Customer
) Q
Group by ID
我有一个名为 Customer 的 table,具有以下架构。
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');
Insert into Customer(id,customer_type,customer_status,account_number)values(243,'PERSONAL','A','02456798');
commit;
我正在尝试获取客户状态为 active.Customer_type 的 Id 可以有两种类型 RETAIL 或 PERSONAL。我只是want return Retail 如果 id 有任何活跃的零售账户,则为真,否则为假, 与 个人 相同 我尝试了以下查询,但我在 returning 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');
预期输出:
|---------------------|------------------|----------------|
| id | Retail | Personal |
|---------------------|------------------|----------------|
| 123 | Y | Y |
|---------------------|------------------|----------------|
| 243 | N | Y |
|---------------------|------------------|----------------|
如果能提供帮助,我们将不胜感激。
尝试使用连接。 以下查询 returns 帐号而不是 'Y'
select C.id, max(R.account_number), max(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'
group by C.id
尝试以下操作(根据您的进一步要求进行修改)。我根据给定的数据执行此操作,但我觉得数据不够广泛,无法测试代码。我做了一些假设,当没有匹配时就把空白。
基本上为了确定 Retail 是 Y 还是 N,我使用了 case 语句并对 Personal 做了同样的事情
-- 该解决方案不适合,因为 OP 需要每个 ID 1 条记录
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
这是所需的解决方案
Select ID, Max(RETAIL) as RETAIL, Max(PERSONAL) as PERSONAL
from
(
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
from Customer
) Q
Group by ID