【问题标题】:SQL - Select rows partially depending on another tableSQL - 部分根据另一个表选择行
【发布时间】:2020-09-25 12:12:39
【问题描述】:

我对数据库和查询非常陌生。 我不确定这是否非常基本。请帮助我找到解决方案。

我有两个表 - 订阅和客户,主键分别为订阅 ID 和客户 ID。 customer_id 是指向客户表 customer_id 列的外键。 以下是表格及其数据的示例:

订阅

subscription_id  customer_id   subscription_start_date   subscription_end_date  subscription_status 
1001             1             1-JAN-2020                31-DEC-2020              PENDING       
1002             2             1-JUN-2020                31-MAY-2021              PENDING       
1003             3             4-JUL-2020                3-JUL-2021               ARCHIVED  
1004             4             2-APR-2020                1-APR-2021               PENDING           
1005             5             3-APR-2020                2-APR-2021               ARCHIVED      
1006             6             21-JAN-2020               20-JAN-2021              PENDING   
1007             7             22-JAN-2020               21-JAN-2021              PENDING   

客户

customer_id membership_type  membership_start_date   membership_status 
1            GOLD             1-JAN-2020             ACTIVE
2            PLATINUM         1-JUN-2020             ACTIVE
3            PLATINUM         5-JUL-2020             PROCESSING
4            GOLD             2-APR-2020             PROCESSING
5            GOLD             3-APR-2020             ACTIVE
6            GOLD             21-JAN-2020            PROCESSING
7            GOLD             22-JAN-2019            EXPIRED

我要查询所有满足以下两个条件的订阅

  1. subscription_satus 处于待处理状态,membership_type 为 GOLD 或 PLATINUM,membership_status 为 ACTIVE。(1001,1002)
  2. 仅当客户 members_type 为 PLATINUM 且membership_start_date 介于subscription_start_date 和subscription_end_date 之间且membership_status 为PROCESING(1003) 时才会归档subscription_status

所以我们应该在标准 1 下得到 1001、1002 条记录。在标准 2 下得到 1003 条记录

【问题讨论】:

  • 1003 与第二个条件的日期条件不匹配。
  • 对不起。修改它。

标签: sql database oracle oracle11g oracle-sqldeveloper


【解决方案1】:

查询应该是这样的;第 1 - 21 行代表样本数据,您无需输入。您确实需要的查询从第 22 行开始,包含 WHERE 子句的两个部分:第一个满足第一个条件,而第二个满足第二个条件。

请注意,用户 3 的示例 MEMBERSHIP_START_DATE02-JUL-2020不在订阅开始 (4-JUL-2020) 和结束 (3-JUL-2021) 日期之间,所以 - 1003不是结果集的一部分。将会员开始日期修改为例如22-JUL-2020 会的。

SQL> with
  2  subscription (subscription_id, customer_id, subscription_start_date,
  3    subscription_end_date, subscription_status) as
  4    (select 1001, 1, date '2020-01-01', date '2020-12-31', 'PENDING'  from dual union all
  5     select 1002, 2, date '2020-06-01', date '2021-05-31', 'PENDING'  from dual union all
  6     select 1003, 3, date '2020-07-04', date '2021-07-03', 'ARCHIVED' from dual union all
  7     select 1004, 4, date '2020-04-02', date '2021-04-01', 'PENDING'  from dual union all
  8     select 1005, 5, date '2020-04-03', date '2021-04-02', 'ARCHIVED' from dual union all
  9     select 1006, 6, date '2020-01-21', date '2021-01-20', 'PENDING'  from dual union all
 10     select 1007, 7, date '2020-01-22', date '2021-01-21', 'PENDING'  from dual
 11    ),
 12  customer (customer_id, membership_type, membership_start_date,
 13    membership_status) as
 14    (select 1, 'GOLD'    , date '2020-01-01', 'ACTIVE'     from dual union all
 15     select 2, 'PLATINUM', date '2020-06-01', 'ACTIVE'     from dual union all
 16     select 3, 'PLATINUM', date '2020-07-02', 'PROCESSING' from dual union all
 17     select 4, 'GOLD'    , date '2020-04-02', 'PROCESSING' from dual union all
 18     select 5, 'GOLD'    , date '2020-04-03', 'ACTIVE'     from dual union all
 19     select 6, 'GOLD'    , date '2020-01-21', 'PROCESSING' from dual union all
 20     select 7, 'GOLD'    , date '2019-01-22', 'EXPIRED'    from dual
 21    )

 22  select s.subscription_id
 23  from subscription s join customer c on c.customer_id = s.customer_id
 24  where (    s.subscription_status = 'PENDING'
 25         and c.membership_type in ('GOLD', 'PLATINUM')
 26         and c.membership_status = 'ACTIVE'
 27        )
 28     or
 29       (     s.subscription_status = 'ARCHIVED'
 30         and c.membership_type = 'PLATINUM'
 31         and c.membership_start_date between s.subscription_start_date
 32                                         and s.subscription_end_date
 33         and c.membership_status = 'PROCESSING'
 34       )
 35  /

SUBSCRIPTION_ID
---------------
           1001
           1002

SQL>

【讨论】:

  • @sree 。 . .这看起来是正确的答案。你应该接受它。
猜你喜欢
  • 1970-01-01
  • 2016-07-22
  • 2021-06-11
  • 2015-02-15
  • 2012-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多