我有这个SQL查询:
SELECT DISTINCT [BatchCode] ,SUM([Quantity]) as 'Created' ,[TotalQuantity] ,[Status] ,[Destination] ,[DateCreated] ,[CreatedBy] FROM [FGIS].[dbo].[DropshipPackinglist] GROUP BY BatchCode, TotalQuantity, Status, Destination, CreatedBy, ModifiedBy, DateCreated
结果是这样的:
BatchCode Created TotalQuantity Status Destination DateCreated CreatedBy --------------------------------------------------------------------------------------------------------------- 0005041007100AHWA11HG 86 86 CREATED MediTelecom S.A. 2010-09-10 00:00:00.000 NULL 0005041007100AHWA11HGK 19 50 ALLOCATED USA 2010-09-12 07:35:17.000 jy 0005041007100AHWA11HGK 31 50 ALLOCATED USA 2010-09-12 07:35:20.000 jy
我现在的问题是我无法将DateCreated分组,因为它具有不同的时间。
我只想按日期分组。范例:2010-09-12
谢谢并恭祝安康…
我认为值得单独发布以下内容:
使用char转换来削减超时日期(广播或转换为varchar)比使用慢DateAdd(Day, DateDiff(Day, 0, DateCreated),0)。我制定了[完整的脚本和性能测试结果以支持此断言
DateAdd(Day, DateDiff(Day, 0, DateCreated),0)
SELECT DISTINCT BatchCode ,SUM(Quantity) as Created ,TotalQuantity ,Status ,Destination ,DateAdd(Day, DateDiff(Day, 0, DateCreated), 0) as DayCreated ,CreatedBy FROM FGIS.dbo.DropshipPackinglist GROUP BY BatchCode, TotalQuantity, Status, Destination, CreatedBy, ModifiedBy, DateDiff(Day, 0, DateCreated) -- note that the DateAdd convert back to datetime is not needed
另外,请注意,您的GROUP BY列表与SELECT列表不同,因此需要进行一些调整。
更新
看来,使用DateAdd和varchar转换所节省的CPU数量虽然相对很多,但绝对不是很多(每行只有几分之一毫秒)。但是,这仍然是性能上的差异,对我来说,最好是尽一切可能。