我有一个如下表格结构。
id,shipment no,status, date
1 , 2 , I, 2013-12-12
2 , 3 , I, 2013-12-12
3 , 3 , S, 2013-12-12
我需要根据日期过滤。我能够做到这一点,但对于给定的货物,如果'S'状态在那里我需要记录,否则我需要采取'我'状态记录。我如何在1个查询中执行此操作?
答案 0 :(得分:1)
试试这个:
SELECT a.id, a.shipmentNo, a.status, a.date
FROM (SELECT a.id, a.shipmentNo, a.status, a.date
FROM shipment a ORDER BY a.shipmentNo, FIELD(a.status, 'S', 'I')
WHERE a.date = '2013-12-12'
) A
GROUP BY a.shipmentNo