SQL查询结果来自7天前

时间:2015-08-14 00:50:59

标签: sql oracle equals

我有一个SQL查询,我需要更新才能从7天前提取结果。目前,查询正在查看7天及之前的结果。我似乎无法找到一个改变>的角色。等于。

SELECT
   distinct(cl.RIID_),
   cl.EMAIL_ADDRESS_  
FROM
   $A$ cl  
JOIN
   $C$ bro 
      ON cl.EMAIL_ADDRESS_ = bro.EMAIL_ADDRESS_   
JOIN
   $D$ cms 
      ON cms.SKU = bro.ITEM  
WHERE
   cl.EMAIL_DELIVERABILITY_STATUS_ = 'D'   
   AND cl.EMAIL_PERMISSION_STATUS_ = 'I'   
   AND (
      cms.CATEGORYNAME = 'Desktop Systems' 
      OR cms.CATEGORYNAME = 'Refurbished Laptops & Tablets' 
      or cms.CATEGORYNAME = 'Laptops & Notebooks' 
   ) 
   AND (
      trunc(bro.ACTIVITY_DATE)
   ) >= (
      Trunc(Cast(From_Tz(Cast(Sysdate As Timestamp ), 'US/Pacific') At Time Zone 'Australia/Sydney' As Date)) - 7
   )

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我看不出如何将>=更改为=会导致该问题。当我将运算符从比较结果中移除时where a而不是where a = b,我会收到此类错误。

但是,您应该将IN用于WHERE子句:

WHERE cl.EMAIL_DELIVERABILITY_STATUS_ = 'D' AND
      cl.EMAIL_PERMISSION_STATUS_ = 'I' AND
      cms.CATEGORYNAME IN ('Desktop Systems', 'Refurbished Laptops & Tablets' , 'Laptops & Notebooks' ) AND
      trunc(bro.ACTIVITY_DATE) = Trunc(Cast(From_Tz(Cast(Sysdate As Timestamp ), 'US/Pacific') At Time Zone 'Australia/Sydney' As Date)) - 7

然后,从distinct左右删除括号。 select distinct适用于所有选定的列。括号只会误导您和其他阅读查询的人。