【问题标题】:Sql Query ModificationSql查询修改
【发布时间】:2020-05-16 16:35:58
【问题描述】:

我有一个名为 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');
    Insert into Customer(id,customer_type,customer_status,account_number)values(243,'PERSONAL','A','02456798');
    commit;

我正在尝试获取客户状态处于活动状态的 Id。Customer_type 可以是两种类型 RETAILPERSONAL。如果 id 有任何活动的 retils,我只想返回 Retail true帐户其他虚假, 与 个人 相同 我尝试了以下查询,但无法返回 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           |
|---------------------|------------------|----------------|

我们将不胜感激。

【问题讨论】:

  • 更新您的问题,添加适当的数据样本和预期结果作为表格文本(不是图像)
  • 更新了问题。

标签: sql oracle oracle11g oracle10g


【解决方案1】:

尝试以下操作(根据您的进一步要求进行修改)。我根据给定的数据做了这个,但我觉得数据不够广泛,无法测试代码。我做了一些假设,当没有匹配时将空白。

基本上为了确定 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

【讨论】:

  • 感谢您的回答,但如果您看到我的预期,即使它有多个帐户,它也只会返回给定 ID 的一行。
  • Distinct 关键字不是必需的(我将编辑删除它)
【解决方案2】:

尝试使用连接。 以下查询返回帐号而不是“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

【讨论】:

  • 感谢您的回答,但如果您看到我的预期,即使它有多个帐户,它也只会返回给定 ID 的一行。
  • 只对结果进行分组
猜你喜欢
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多