PostgreSQL,功能最后在哪里?

标签 postgresql

又一次出现了我不知道如何解决的问题,因为我缺乏 SQL 知识。让我“画”出来。

DROP TABLE IF EXISTS mytry; 
CREATE TABLE IF NOT EXISTS mytry 
   (mydate timestamp, doctype text, docnum integer, inprice decimal, 
    price decimal, inqty decimal, outprice decimal, outqty decimal); 
INSERT INTO mytry (mydate, doctype, docnum, inprice, price, 
                   inqty, outprice, outqty) VALUES 
('2017-01-01 00:00:00','BegDoc',   0, 13.24, 23.00, 10,     0, 0), 
('2017-01-02 17:18:14','OutDoc',  10,     0,     0,  0, 25.00, 2), 
('2017-01-03 14:04:39','OutDoc',  16,     0,     0,  0, 25.00, 2), 
('2017-01-03 14:36:20','OutDoc',  17,     0,     0,  0, 25.00, 1), 
('2017-01-03 14:58:45','OutDoc',  18,     0,     0,  0, 25.00, 2), 
('2017-01-05 14:39:06','InDoc',   25, 27.20, 34.00,  9,     0, 0), 
('2017-01-11 14:01:33','OutDoc',  57,     0,     0,  0, 34.00, 2), 
('2017-01-13 13:05:44','OutDoc',  74,     0,     0,  0, 34.00, 2), 
('2017-01-13 14:38:40','OutDoc',  75,     0,     0,  0, 34.00, 1), 
('2017-01-16 13:43:35','OutDoc',  88,     0,     0,  0, 34.00, 1), 
('2017-01-17 13:24:10','OutDoc',  95,     0,     0,  0, 34.00, 2), 
('2017-01-18 18:24:30','InDoc',   79, 20.40, 29.60, 20,     0, 0), 
('2017-01-19 17:31:55','OutDoc2',  6,     0,     0,  0, 29.60, 2), 
('2017-01-25 10:53:10','OutDoc', 121,     0,     0,  0, 29.60, 1), 
('2017-01-26 15:17:06','OutDoc2', 13,     0,     0,  0, 29.60, 2), 
('2017-01-28 09:39:05','OutDoc', 128,     0,     0,  0, 29.60, 3), 
('2017-01-28 11:03:42','OutDoc', 138,     0,     0,  0, 29.60, 2), 
('2017-02-03 16:08:23','OutDoc2', 16,     0,     0,  0, 29.60, 2); 

简而言之,这是一个“ Material 卡”表,它根据文档类型和文档编号显示商店中特定 Material 更改的任何更改。这里有两种主要类型的文件。文件的输入和输出类型。文档在创建时有他的时间戳。之后,我们在这些文件中定义了输入或输出价格以及输入或输出数量。

问题是我必须在表中不存在的时间戳上查询此表。像这样:

SELECT MAX(mydate) AS lastdate, MAX(doctype) AS lastdoctype, MAX(docnum) AS lastdocnum, 
   LAST(inprice) AS lastinprice, LAST(price) AS lastprice, SUM(inqty-outqty) AS lastqty
 FROM mytry 
WHERE mydate<'2017-01-26 14:00:00' 
LIMIT 1;

结果是什么:

"2017-01-25 10:53:10";"OutDoc2";121;0;0;21

其中 lastdate、lastdoctype、lastdocnum 和 lastqty 是正确的(预期的)值,但 lastinpricelastprice 不是。此查询的目的是获取查询所说的准时文章的确切情况。在展示的情况下看起来像:

"2017-01-25 10:53:10";"OutDoc2";121;20.40;29.60;21

这意味着查询应该查找大于 0 的列 lastinprice 和 lastprice 最后一个值。如果我有 LAST(inprice) WHERE inprice>0 函数,那将是理想的,但我没有。或者我有?

如果有人可以根据我对问题的描述为 lastinprice 和 lastprice 列生成预期结果的查询,请给我。

最后,这里有一些我为测试(手工)制作的查询和想要的查询结果的时间示例。

'Time in query               Wanted result
'----------------------------------------------------------------------------
'2017-01-02 19:00:00'        "2017-01-02 17:18:14";"OutDoc";10;13.24;23.00;8
'2017-01-07 12:00:00'        "2017-01-16 13:43:35";"OutDoc";88;27.20;34.00;12
'2017-01-18 20:00:00'        "2017-01-18 18:24:30";"OutDoc";95;20.40;29.60;24
'2017-01-29 15:00:00'        "2017-01-28 11:03:42";"OutDoc2";138;20.40;29.60;14

编辑: 现在,当我很好地解释问题时 :) 我自己找到解决方案。 这是关于性能的最佳解决方案还是可能更好/更快?

SELECT LAST(mydate) AS lastdate, LAST(doctype) AS lastdoctype, LAST(docnum) AS lastdocnum, 
   (SELECT LAST(inprice) AS lastinprice FROM mytry WHERE inprice>0 AND mydate<'2017-01-18 20:00:00') , 
   (SELECT LAST(price) AS lastprice FROM mytry WHERE price>0 AND mydate<'2017-01-18 20:00:00'), 
   SUM(inqty-outqty) AS lastqty
 FROM mytry 
WHERE mydate<'2017-01-18 20:00:00' 
LIMIT 1;

最佳答案

使用LAST() with FILTER:

SELECT 
    MAX(mydate) AS lastdate, 
    MAX(doctype) AS lastdoctype, 
    MAX(docnum) AS lastdocnum, 
    LAST(inprice ORDER BY mydate) FILTER (WHERE inprice <> 0) AS lastinprice, 
    LAST(price ORDER BY mydate) FILTER (WHERE price <> 0) AS lastprice, 
    SUM(inqty-outqty) AS lastqty
FROM mytry 
WHERE mydate < '2017-01-26 14:00:00' 

关于PostgreSQL,功能最后在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42648927/

相关文章:

postgresql - 无法连接到 docker 中的 postgres 服务器

sql - 在 PostgreSQL 中按版本号解析记录

postgresql - 清理最旧的最大行

sql - 有可能 "Select N times"吗?

node.js - Loopback 4 从数据库中发现模型

asp.net - 类中的异常处理和 C# 的代码隐藏

postgresql - 在 postgresql 中创建断言

sql - PostgreSQL:使用 psql 命令行实用程序时 Windows 上的编码问题

PostgreSQL 性能 : keep seldomly used small database in memory while server busy with big database

PostgreSQL、libpq/C、更新参数