我试图通过加入item_num键从两个表 campinging 和 sales 中提取数据。
With campaing as
(Select distinct item_num,campaing_id,campaing_name,campaing_start_date,campaing_end_date
from Campaing_event
where camping_start_date > to_date ('20190101', 'yyyymmdd')
)
上述所有广告活动的开始和结束数据都不同,如何使用上述广告活动的开始和结束日期从销售表中获取这些广告活动的销售数据。
谢谢。
答案 0 :(得分:0)
我想您的sales
表中有一个sold_on
日期列。如果是这种情况,那么您的查询应如下所示:
with
c as (
select distinct
item_num, campaing_id, campaing_name,
campaing_start_date, campaing_end_date
from campaing_event
where camping_start_date > to_date ('20190101', 'yyyymmdd')
)
select c.*, s.*
from c
join sales s on s.item_num = c.item_num
and s.sold_on >= s.campaing_start_date
and s.sold_on <= s.campaing_end_date
答案 1 :(得分:0)
由于您未在SALES表中指定列,因此我将SALES_DATE用于该列。 您可以使用此查询根据您的模式对其进行修改:
基于存在
With campaing as
(Select distinct item_num,campaing_id,campaing_name,campaing_start_date,campaing_end_date
from Campaing_event
where camping_start_date > str_to_date ('20190101', '%Y%m%d')
)
SELECT * FROM SALES T1 WHERE 1=1
AND EXISTS(
SELECT 1 FROM CAMPAING T2
WHERE T1.ITEM_NUM = T2.ITEM_NUM
AND T1.SALES_DATE BETWEEN T2.campaing_start_date AND T2.campaing_end_date
)
基于加入
With campaing as
(Select distinct item_num,campaing_id,campaing_name,campaing_start_date,campaing_end_date
from Campaing_event
where camping_start_date > str_to_date ('20190101', '%Y%m%d')
)
SELECT * FROM SALES T1 join CAMPAING T2
on T1.ITEM_NUM = T2.ITEM_NUM
AND T1.SALES_DATE BETWEEN T2.campaing_start_date AND T2.campaing_end_date