![](/img/trans.png)
[英]How to select certain rows with an sql statement depending on whats in another table
[英]SQL - Select rows partially depending on another table
我對數據庫和查詢非常陌生。 我不確定這是否非常基本。 請幫我找到解決方案。
我有兩個表 - 訂閱和客戶,主鍵分別為訂閱 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 下獲得 1001、1002 條記錄。在標准 2 下獲得 1003 條記錄
查詢應該是這樣的; 第 1 - 21 行代表樣本數據,您無需輸入。 您確實需要的查詢從第 22 行開始,包含WHERE
子句的兩個部分:第一個滿足第一個條件,而第二個滿足第二個條件。
請注意,curstomer 3
的示例MEMBERSHIP_START_DATE
是02-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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.