只获取上周和上周的最新数据并总结一些列

时间:2017-07-06 02:02:20

标签: sql postgresql select

仅获取上周的最新数据并总结一些专栏

我用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周前的数据,并且会有来自上周的数据,在这种情况下,本周它可能为空或空,这也可能发生反之亦然(来自上个星期)。

    --                                      | 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 个答案:

答案 0 :(得分: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