查询是为了查找续订订单
续订是任何学校在上一学年的订单(即total_line_price> 0),但同一所学校已购买(支付 - 总行价> 0)另一个订单。
明年'08 -01-2016 00:00:00'和'07 -31-2017 00:00:00'-2016
当前年份'08 -01-2015 00:00:00'和'07 -31-2016 00:00:00'-2015
去年'08 -01-2014 00:00:00'和'07 -31-2015 00:00:00' - 2014
下面是我写的查询,不对。需要一些帮助
select
(school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where school_ucn not in ((select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where (((start_date between '08-01-2014 00:00:00' and '07-31-2015 00:00:00')
and (total_line_price >0) ))
and in
(select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where ((start_date between '08-01-2015 00:00:00' and '07-31-2016 00:00:00') and ( total_line_price >0))
)))
答案 0 :(得分:0)
您在查询中使用and in
。它应该是and school_ucn in
。
始终正确格式化您的查询以便轻松分析。
select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where school_ucn not in (
select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where start_date between '08-01-2014 00:00:00' and '07-31-2015 00:00:00'
and total_line_price >0)
and school_ucn in (select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order
where start_date between '08-01-2015 00:00:00' and '07-31-2016 00:00:00'
and total_line_price >0)
使用Not Exists,您可以按如下方式重写查询。
select school_ucn
from storiacloud_staging.schl_royl_vw_edw_oms_order as a
where start_date between '08-01-2015 00:00:00' and '07-31-2016 00:00:00'
and total_line_price >0
and not exists (select 1
from storiacloud_staging.schl_royl_vw_edw_oms_order as b
where b. school_ucn = a. school_ucn
and start_date between '08-01-2014 00:00:00' and '07-31-2015 00:00:00’
and total_line_price >0)