抓住数据超过一天

时间:2015-01-05 03:31:54

标签: sql postgresql

select CASE 
WHEN (shipment_date >=((to_char(shipment_date, 'dd-Mon-yyyy 08:00')::timestamp)) and shipment_date < ((to_char(shipment_date, 'dd-Mon-yyyy 20:00')::timestamp )))
THEN to_char(shipment_date, 'dd/MM/yyyy 08:00') 
WHEN (shipment_date >=((to_char(shipment_date, 'dd-Mon-yyyy 20:00')::timestamp)) and shipment_date < ((to_char(shipment_date + interval '1' day, 'dd-Mon-yyyy 08:00')::timestamp )))
THEN to_char(shipment_date, 'dd/MM/yyyy 20:00')
END as shipment_date_sort,
COUNT(lotnumber) 
from shipping.picklist_lots 
inner join shipping.picklist on picklist_lots.picklist_id = picklist.picklist_id 
where shipment_date >= '12/23/2014' and shipment_date < '12/24/2014'
GROUP BY shipment_date_sort

它没有给我的结果是24/12/2014 00:00到24/12/2014 08:00之间的发货日期

我需要它

2 个答案:

答案 0 :(得分:0)

where shipment_date >= '12/23/2014' and shipment_date < '12/24/2014'

添加时间值。根据您的日期时间值,24/12/2014 00:00,它应该是格式(年/月/日00:00)

where shipment_date >= '23/12/2014' and shipment_date <= '24/12/2014 23:59'

答案 1 :(得分:0)

根据逻辑查询处理,您不能在分组中使用SELECT语句的别名,因为SELECTGROUP BY之后发生,因此您需要将CASE语句复制到你的GROUP BY部分。

LOGICAL QUERY PROCESSING