每个实例的最大日期/时间

时间:2013-07-05 15:02:51

标签: sql max

早上好: 我有这个查询,但我需要它给我每个订单的最后日期/时间。订单有时会在每个日期多次处理...我只想要最后一个实例。这是查询...真的很感激任何帮助...提前谢谢

SELECT      dbo.fn_GetNotesForOrder(o.orderid) as Notes, o.OrderNumber, ss.statusname, max(oh.EntryDate),  l.locationcode, l.locationname, a.street, a.city, a.province, a.postalcode, oh.UserID

FROM         orders as o
                               INNER JOIN
             OrderHistory as oh on o.OrderID = oh.OrderID
                               INNER JOIN 
             Statuses as ss on ss.StatusID = oh.Status
                               INNER JOIN   
             addresses as a on a.addressid = oh.addressid
                               INNER JOIN
             locations as l on l.locationid = a.locationid

             WHERE     (oh.EntryDate >= CONVERT(DATETIME, '2013-06-24 00:00:00', 102) AND oh.EntryDate < CONVERT(DATETIME, '2013-06-29 00:00:00', 102)) AND 
                      (UserID in ('MAXWELLPAPER\leyre'/*, 'MAXWELLPAPER\ndwyer', 'MAXWELLPAPER\jploeger', 'MAXWELLPAPER\jpquintanilla', 'MAXWELLPAPER\dwatkinson'*/))

             group by o.orderid, o.ordernumber, ss.statusname, l.locationname, l.locationcode, a.street, a.city, a.province, a.postalcode, oh.UserID, oh.entrydate
             order by oh.entrydate

0 个答案:

没有答案