日期比较SQL

时间:2015-10-01 07:26:27

标签: sql date teradata difference

我正在处理一个数据集,我必须将一天的收入与上周的同一天进行比较。

我已经分享了数据样本:

Date    DAY_IN_WEEK_NAME    onnetMins
9/18/2015   FRIDAY  311,980,365.00
9/25/2015   FRIDAY  361,232,362.00
9/21/2015   MONDAY  299,167,025.50
9/28/2015   MONDAY  292,725,603.00
9/19/2015   SATURDAY    310,260,553.00
9/26/2015   SATURDAY    314,627,373.50

对于SQL编写,我尝试过如下查询:

sel
 (case when day_in_week_name = 'Friday' then  
 (distinct (onnetMins)- distinct (onnetMins)) end) 
 from MYTABLE group by 1
然而,它不起作用。

任何人都可以尝试调整这个sql吗?

1 个答案:

答案 0 :(得分:1)

这是可能对您有所帮助的查询:

SELECT
    t1.DAY_IN_WEEK_NAME
    , t2.onnetMins - t1.onnetMins   
FROM
    MYTABLE t1
    INNER JOIN MYTABLE t2
    ON t1.DAY_IN_WEEK_NAME = t2.DAY_IN_WEEK_NAME
WHERE
    t1.date < t2.date