仅获取上周和过去上周的最新数据并汇总某些列

Posted

技术标签:

【中文标题】仅获取上周和过去上周的最新数据并汇总某些列【英文标题】:Get only latest data from last week and past last week and sum some column 【发布时间】:2017-07-06 02:02:20 【问题描述】:

仅获取上周的最新数据并对某列求和

我用dat做了一个例子,实际结果和预期的结果。

http://rextester.com/HMB12638

--Taking an example kind of like this..

--      user    contact         barcode date                in  out dif
-- 1    USER2   Guillermo Tole  987654  16.06.2017 05:27:00 500 420 80
-- 2    USER2   Guillermo Tole  281460  15.06.2017 05:36:00 310 220 90
-- 3    USER2   Guillermo Tole  987654  13.06.2017 05:27:00 400 380 20
-- 4    USER2   Guillermo Tole  281460  12.06.2017 05:26:00 230 190 40
-- 5    USER3   Juan Rulfo      123456  15.06.2017 05:37:00 450 300 150
-- 6    USER3   Juan Rulfo      123456  12.06.2017 05:37:00 450 300 150
-- 7    USER3   Pepito Marquez  346234  15.06.2017 05:37:00 600 360 240
-- 8    USER3   Pepito Marquez  346234  14.06.2017 05:37:00 450 300 150

这是我使用此查询的实际结果。

首先。创建一个表并保留您想要的 ID 信息

with tabla as (
    SELECT distinct on( barcode) barcode  as barcode, id, date
    from table1 as tabla
    where date_trunc('day', date) <= '2017-06-25' ::date - (interval '1 week')::interval 
 and date >  '2017-06-25'::date - (interval '2 weeks')::interval
    order by barcode, date desc
) 

然后在之前创建的表上使用内连接进行查询

select user, contact, t1.barcode, t1.date, "in", out, dif ,  sum("in" - out) over (partition by contact order by t1.barcode)
    from table1 t1 
      inner join tabla on tabla.id = t1.id
    where date_trunc('day', t1.date) <= '2017-06-25' ::date - (interval '1 week')::interval 
 and t1.date >  '2017-06-25'::date - (interval '2 weeks')::interval 
 order by contact, barcode, date desc
 -- PD, "in" is a reserved word, i have to keep it with commas

这是我使用上一个查询得到的结果。

--      user    contact         barcode date                in  out sum
-- 1    USER2   Guillermo Tole  987654  16.06.2017 05:27:00 500 420 170 (80 + 90)
-- 2    USER2   Guillermo Tole  281460  15.06.2017 05:36:00 310 220 170 (80 + 90)
-- 5    USER3   Juan Rulfo      123456  15.06.2017 05:37:00 450 300 150
-- 7    USER3   Pepito Marquez  346234  15.06.2017 05:37:00 600 360 240

这是预期的结果,有时不会有 2 周前的数据,而会有上周的数据,在这种情况下,它可以为 null 或本周为空,反之亦然(来自上个星期)。

    --                                      | 2 weeks ago-----------------| | last week ------------------|
    --      user    contact         barcode date                in  out sum date                in  out sum     
    -- 1    USER2   Guillermo Tole  987654  8.06.2017 05:27:00  500 420 170 15.06.2017 05:27:00 600 550 100 
    -- 2    USER2   Guillermo Tole  281460  6.06.2017 05:36:00  310 220 170 16.06.2017 05:27:00 400 350 100
    -- 5    USER3   Juan Rulfo      123456  9.06.2017 05:37:00  450 300 150 14.06.2017 05:27:00 650 350 300
    -- 7    USER3   Pepito Marquez  346234  7.06.2017 05:37:00  600 360 240 15.06.2017 05:27:00 750 500 250

【问题讨论】:

【参考方案1】:

这是an earlier answer 的变体,现在可以通过将条形码包含到 LAG() 和 ROW_NUMBER() 的 OVER() 子句中来工作(我相信这是正确的)。

select "user", "contact", "barcode", "prev2date", "prev2in", "prev2out","prev2dif", "prev1date", "prev1in", "prev1out","prev1dif"
     , sum("prev1in"-"prev1out") over(partition by "user", "contact") as "sum"
from (
    select "user", "contact", "barcode", "date", "in", "out","dif"
    , lag("date",2) over(partition by "user", "contact", "barcode" order by "date" ASC)  prev2date
    , lag("in"  ,2) over(partition by "user", "contact", "barcode" order by "date" ASC)  prev2in
    , lag("out" ,2) over(partition by "user", "contact", "barcode" order by "date" ASC)  prev2out
    , lag("dif" ,2) over(partition by "user", "contact", "barcode" order by "date" ASC)  prev2dif
    , lag("date",1) over(partition by "user", "contact", "barcode" order by "date" ASC)  prev1date
    , lag("in"  ,1) over(partition by "user", "contact", "barcode" order by "date" ASC)  prev1in
    , lag("out" ,1) over(partition by "user", "contact", "barcode" order by "date" ASC)  prev1out
    , lag("dif" ,1) over(partition by "user", "contact", "barcode" order by "date" ASC)  prev1dif
    , row_number()  over(partition by "user", "contact", "barcode" order by "date" DESC) rn
    from "table1" 
    ) d
where rn = 1 and prev1dif is not null
order by 1,2,4 DESC

根据您的示例数据,使用上面的查询我得到了这个结果:

+----+-------+----------------+---------+---------------------+---------+----------+----------+---------------------+---------+----------+----------+-----+
|    | user  |    contact     | barcode |      prev2date      | prev2in | prev2out | prev2dif |      prev1date      | prev1in | prev1out | prev1dif | sum |
+----+-------+----------------+---------+---------------------+---------+----------+----------+---------------------+---------+----------+----------+-----+
|  1 | USER2 | Guillermo Tole |  987654 | 23.06.2017 05:27:00 | 700     | 690      | 10       | 28.06.2017 05:27:00 |     800 |      760 |       40 | 120 |
|  2 | USER2 | Guillermo Tole |  281460 | 15.06.2017 05:36:00 | 310     | 220      | 90       | 20.06.2017 09:37:00 |     490 |      410 |       80 | 120 |
|  3 | USER3 | Juan Rulfo     |  123456 | NULL                | NULL    | NULL     | NULL     | 12.06.2017 05:37:00 |     450 |      300 |      150 | 150 |
|  4 | USER3 | Pepito Marquez |  346234 | 27.06.2017 05:37:00 | 900     | 690      | 210      | 30.06.2017 05:37:00 |    1050 |      900 |      150 | 150 |
+----+-------+----------------+---------+---------------------+---------+----------+----------+---------------------+---------+----------+----------+-----+

http://rextester.com/WODBE20956

【讨论】:

感谢您的回答,是的,这是其他问题的继续,我非常感谢,现在我做了一些观察,认为第 4 行是错误的,两个日期都来自同一周,在第 2 行该条形码有上周的数据,它显示的是 2 周和 3 周前的数据。第 3 行显示的是 3 周前的数据,应该在左侧“列”

以上是关于仅获取上周和过去上周的最新数据并汇总某些列的主要内容,如果未能解决你的问题,请参考以下文章

需要从nvarchar列上周选择

提取上周数据(而不是过去7天)

SQL Server 按过去 24 小时、上周和上个月对促销进行分组,并按周降序排序

一周热文和最新 JS 进阶面试题 #4

如何在红移中获取过去 X 周的数据?

MySQL Query 选择上周的数据?