获取上周唯一的最新数据并对某些列求和

2024-01-12

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

我用数据、实际结果和预期做了一个例子。

http://rextester.com/HMB12638 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

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

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

Result:

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

See: http://rextester.com/ISHS42170 http://rextester.com/ISHS42170

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


+EDIT

而不是使用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 http://rextester.com/SCV98254

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取上周唯一的最新数据并对某些列求和 的相关文章

  • Postgresql 致命数据库系统正在启动 - Windows 10

    我已经安装了postgresql on windows 10 on usb disk 每天当我启动电脑工作时sleep并再次插入磁盘然后尝试启动postgresql我收到这个错误 FATAL the database system is s
  • 如何获取 JDBC 中 UPDATE 查询影响的所有行?

    我有一项任务需要使用更新记录PreparedStatement 一旦记录被更新 我们知道更新查询返回计数 即受影响的行数 但是 我想要的不是计数 而是受更新查询影响的行作为响应 或者至少是受影响的行的 id 值列表 这是我的更新查询 UPD
  • 使用 PostgreSQL 的模式和 Rails 创建多租户应用程序

    我已经想通的事情 我正在学习如何在 Rails 中创建多租户应用程序 该应用程序根据用于查看应用程序的域或子域来提供来自不同模式的数据 我已经回答了一些问题 如何让 subdomain fu 也能与域一起使用 这是有人问了同样的问题 htt
  • MySQL 查询中的窗口函数

    有没有办法在 SELECT 查询本身中动态地使用 MySQL 查询中的窗口函数 我知道在 PostgreSQL 中这是可能的 例如 下面是 PostgreSQL 中的等效查询 SELECT c server ip c client ip s
  • LINQ 对特定属性的 Distinct()

    我正在玩 LINQ 来了解它 但我不知道如何使用Distinct https learn microsoft com en us dotnet api system linq enumerable distinct当我没有一个简单的列表时
  • Python在postgresql表中查找带有单引号符号的字符串

    我需要从 psql 表中查找包含多个单引号的字符串 我当前的解决方案是将单引号替换为双单引号 如下所示 sql query f SELECT exists SELECT 1 FROM table name WHERE my column m
  • 在 SELECT 中将列值拆分为两列?

    我在 varchar 列中有一个字符串值 它是一个由两部分组成的字符串 在它到达数据库之前分割它不是一个选择 该列的值如下所示 one column part1 part2 part1 part2 所以我想要的是一个如下所示的结果集 col
  • 将 CSV 复制到 Amazon RDS 托管的 Postgresql 数据库

    我有一个使用 Amazon 的 RDS 服务托管的数据库 我正在尝试编写一个 Web 服务来更新所述数据库 我遇到的问题是它不允许我使用 COPY 命令 因为我收到此错误 错误 必须是超级用户才能复制到文件或从文件复制 我正在使用我为数据库
  • 为什么 hibernate 在一张表中保存两个 @OneToMany 列表?

    想象一下使用 Hibernate 和 JPA 的简化代码如下 Entity class C Id GeneratedValue public long id MappedSuperclass abstract class A Id Gene
  • 使用连接查询检索行

    我有两张这样的桌子 A B col1 col2 col1 col2 一个表包含 300k 行 B表包含400k行 如果表 A 的 col1 与表 B 的 col1 匹配 我需要计算它 我写了一个这样的查询 select count dist
  • 从备份恢复 PostgreSQL 数据库,没有外键约束问题

    我有一个包含大约 85 个以上表的 postgresql 数据库 我定期使用pg dump 通过 php pgadmin 在复制模式下 备份文件的大小几乎为 10 12 MB 现在我面临的问题是 每当我尝试恢复数据库时 都会出现外键约束问题
  • 在同一个表上组合两个 SQL SELECT 语句

    我想结合这两个 SQL 查询 SELECT FROM Contracts WHERE productType RINsell AND clearTime IS NULL AND holdTime IS NOT NULL ORDER BY g
  • 在 postgresql 9.4 或 9.5 中查询 json 对象的嵌套数组中的元素

    studentID 1 StudentName jhon Data schoolname school1 enrolmentInfo year 2015 info courseID csc213 school IT enrollmentda
  • POSTGRESQL:如何在现有表上添加包含数据的新列

    我是 postgres 和 sql 脚本的新手 所以请耐心等待 我想要做的是在现有表上添加包含数据的现有列 现有表的示例 NAME AGE Adam 25 Tim 30 现在我想添加一个新列 ADDRESS 其中包含 Adam 和 Tim
  • 在 Postgres 中的数组字段上应用聚合函数?

    是否可以对整数 字段 或其他数字数组 中的所有值应用聚合 如 avg stddev CREATE TABLE widget measurement integer insert into widget measurement values
  • 使用包含空值列的 WHERE 子句的更新语句

    我正在使用另一个表中的数据更新一个表上的列 这WHERE子句基于多个列 并且某些列为空 根据我的想法 这个空值是什么throwing off你的标准UPDATE TABLE SET X Y WHERE A B陈述 See 这个 SQL 小提
  • 根据 PostgreSQL 中的列将文本附加到列数据

    我想将一些文本附加到表格每一列的每个单元格中 作为该特定列的符号 例如 假设我的表如下 所有字段的类型字符不同 name age location james 45 france simon 33 usa ben 76 china 我想将其
  • pg gem Trace/BPT 陷阱:MAC OS X lion 上出现 5 错误

    我最近将我的雪豹升级为狮子 在我的一个应用程序中我得到了 gt Booting WEBrick gt Rails 3 0 4 application starting in development on http 0 0 0 0 4000
  • postgres - 错误:运算符不存在

    再说一次 我有一个在本地运行良好的函数 但是将其转移到网上会产生一个很大的错误 从有人指出我传递的参数数量不准确的响应中得到提示 我双重 在这种情况下检查以确保我向函数本身传递了 5 个参数 Query failed ERROR opera
  • Psycopg / Postgres:连接随机挂出

    我正在使用 psycopg2 作为我当前正在开发的cherrypy 应用程序 并使用 cli 和 phpgadmin 来手动处理一些操作 这是Python代码 One connection per thread cherrypy threa

随机推荐