COUNT CASE语句返回不需要的查询结果

时间:2017-04-20 20:25:11

标签: sql count case teradata

以下查询在事件为“常规”时查找按对象计算的天数,但查询结果显示“清除”,因为该对象属于指定的日期范围。有没有办法让这个查询只显示在日期范围4/10/2017到今天有“常规”事件的对象?我已经尝试过Event not IN C或S并在where子句中移动Event,但得到相同的结果。

表1:

Cal_Date    Object  Event    Time Stamp Time Stamp
4/10/2017   Soap    Regular     4/10/2017   4/10/2017
4/11/2017   Soap    Sale        4/11/2017   4/14/2017
4/12/2017   Soap    Sale        4/11/2017   4/14/2017
4/13/2017   Soap    Sale        4/11/2017   4/14/2017
4/14/2017   Soap    Sale        4/11/2017   4/14/2017
4/15/2017   Soap    Regular     4/15/2017   4/20/2017
4/16/2017   Soap    Regular     4/15/2017   4/20/2017
4/17/2017   Soap    Regular     4/15/2017   4/20/2017
4/18/2017   Soap    Regular     4/15/2017   4/20/2017
4/19/2017   Soap    Regular     4/15/2017   4/20/2017
4/20/2017   Soap    Regular     4/15/2017   4/20/2017
4/10/2017   Shampoo Clearance   4/10/2017   12/31/2017
4/11/2017   Shampoo Clearance   4/10/2017   12/31/2017
4/12/2017   Shampoo Clearance   4/10/2017   12/31/2017
4/13/2017   Shampoo Clearance   4/10/2017   12/31/2017
4/14/2017   Shampoo Clearance   4/10/2017   12/31/2017
4/15/2017   Shampoo Clearance   4/10/2017   12/31/2017
4/16/2017   Shampoo Clearance   4/10/2017   12/31/2017
4/17/2017   Shampoo Clearance   4/10/2017   12/31/2017
4/18/2017   Shampoo Clearance   4/10/2017   12/31/2017
4/19/2017   Shampoo Clearance   4/10/2017   12/31/2017
4/20/2017   Shampoo Clearance   4/10/2017   12/31/2017


SELECT
    OBJECT
    COUNT(CASE WHEN EVENT='Regular'AND CAL_DATE BETWEEN DATE '4/10/2017' AND CURRENT_DATE THEN 1 END) AS DAYS
FROM TABLE 1
GROUP BY
    OBJECT

查询结果:

Object  Days
Soap    7
Shampoo ?

期望的结果:

Object Days
Soap    7

1 个答案:

答案 0 :(得分:-1)

试试这个。

patternlab - molecules