【问题标题】:Get the only latest data from last week and sum some column获取上周唯一的最新数据并对某列求和
【发布时间】:2017-07-05 00:17:55
【问题描述】:

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

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

http://rextester.com/HMB12638

 --Taking first as example..

--      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


 -- This would be the expectation
-- (MOST RECENT in . out) SUM of all the barcodes showed
--      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

【问题讨论】:

    标签: postgresql distinct


    【解决方案1】:

    我认为这符合您的预期结果:

    select "user", "contact", "barcode", "date", "in", "out","dif"
         , sum("in"-"out") over(partition by "user", "contact") as "sum"
    from (
        select "user", "contact", "barcode", "date", "in", "out","dif"
        , lag(dif,1) over(partition by "user", "contact" order by "date" ASC) prevdif
        , row_number() over(partition by "user", "contact" order by "date" DESC) rn
        from "table1" 
        where date_trunc('day', "date") <= '2017-06-25' ::date - (  interval '1 week')::interval 
        and "date" >  '2017-06-25'::date - (  interval '2 weeks')::interval 
        ) d
    where rn in (1,2) and prevdif is not null
    order by 1,2,4 DESC
    

    结果:

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

    见:http://rextester.com/ISHS42170

    对于诸如“最近”之类的条件,我发现使用 ROW_NUMBER() OVER() 是最方便的,因为它允许返回每个“最近”事件的整行,如果使用 MAX,这并不是那么简单() 和分组依据。通过过滤函数返回值为 1 的行返回“不同”结果。


    +编辑

    而不是使用where rn in (1,2),我相信更好的方法是在 OVER(PARTITION BY...) 条件下使用条形码,如下所示:

    select "user", "contact", "barcode", "date", "in", "out","dif"
         , sum("in"-"out") over(partition by "user", "contact") as "sum"
    from (
        select "user", "contact", "barcode", "date", "in", "out","dif"
        , lag(dif,1) over(partition by "user", "contact", "barcode" order by "date" ASC) prevdif
        , row_number() over(partition by "user", "contact", "barcode" order by "date" DESC) rn
        from "table1" 
        where date_trunc('day', "date") <= '2017-06-25' ::date - (  interval '1 week')::interval 
        and "date" >  '2017-06-25'::date - (  interval '2 weeks')::interval 
        ) d
    where rn = 1 and prevdif is not null
    order by 1,2,4 DESC
    

    http://rextester.com/SCV98254

    【讨论】:

    • 这真是……太棒了!我不是这方面的专家
    • 好的,我一直在用这个进行测试,如果我从 1-2 周更改为 1-3、4、5 等周,它会带来重复的条形码结果,我只想最后一个。
    • 你可以测试我的,它不会带来重复的条形码,只是最新的。
    【解决方案2】:

    做到了..以防万一有人需要它,它可能不是最优雅的工作,但至少它有效

    -- create a table and keep the IDs you want the info
    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
    ) 
    -- make the query using inner join on the previously created table
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 2014-03-14
      • 1970-01-01
      相关资源
      最近更新 更多