取得7天最久的记录

时间:2018-09-28 08:27:00

标签: sql postgresql

我查询了最早7天的获取数据,但失败了,提示“之间”。我的查询:

SELECT b.sequence_number, b.message
FROM test.tab1 a, test.tab2 b
WHERE a.id = b.id 
AND a.delivery_date between (select min(delivery_date) from test.tab1) 
and (select min(delivery_date)-7 from test.tab1)

,但此查询无法获得结果。如何解决此问题?

2 个答案:

答案 0 :(得分:2)

您需要确定订单之间的顺序,较小的应该是第一个,然后是较大的

SELECT b.sequence_number, b.message
FROM test.tab1 a, test.tab2 b
WHERE a.id = b.id 
AND a.delivery_date between (select min(delivery_date)-7 from test.tab1)
and (select min(delivery_date) from test.tab1) 

答案 1 :(得分:0)

如果您希望从当前日期开始最近7天的记录,则查询为

    SELECT b.sequence_number, b.message
    FROM test.tab1 a, test.tab2 b
    WHERE a.id = b.id 
    AND a.delivery_date>=current_date - interval '7 days'

但是如果要从您的最短日期开始7天,那应该是

SELECT b.sequence_number, b.message
        FROM test.tab1 a, test.tab2 b
        WHERE a.id = b.id 
        AND a.delivery_date>=(select min(delivery_date)-7 from test.tab1) and a.delivery_date <=(select min(delivery_date) from test.tab1)