SELECT [order id],
pickingdate
FROM td_order1
WHERE sku = xyz
GROUP BY pickingdate,
[order id]
HAVING pickingdate >= Min(pickingdate)
AND pickingdate <= Max(pickingdate)
ORDER BY pickingdate
此查询有什么问题吗?
答案 0 :(得分:1)
查询的问题是您正在使用having
中未聚合的列。我的建议是使用窗口功能:
SELECT o.*
FROM (SELECT o.*,
MIN(pickingdate) OVER (PARTITION BY sku) as min_pd,
MAX(pickingdate) OVER (PARTITION BY sku) as max_pd
FROM td_order1 o
WHERE sku = xyz
) o
WHERE pickingdate > min_pd and pickingdate < max_pd
ORDER BY pickingdate;
答案 1 :(得分:0)
将条件修改为:
pickingdate < Max(pickingdate)
排除结束日期