我遇到了 Group by DATEPART() 子句问题。我的代码是
SELECT con.Name, con.contractID, Sum(cplanit.Cost) as costs, DATENAME(month,month(cplanit.PaymentDate)) as month, year(cplanit.PaymentDate) as year
FROM ContractTable con
INNER JOIN [a lot of tables] ON [joins proofed and working fine]
Group By con.Name, con.contractID, DATEPART(month,cplanit.PaymentDate), DATEPART(year,cplanit.PaymentDate)
我得到这个:
Adobe CLP Agreement - Initial purchase C00012121 18.7500 January 2011
Adobe CLP Agreement - Initial purchase C00012121 18.7500 January 2010
Adobe CLP Agreement - Initial purchase C00012121 18.7500 January 2011
Adobe CLP Agreement - Initial purchase C00012121 18.7500 January 2010
Adobe CLP Agreement - Initial purchase C00012121 18.7500 January 2011
相同的条目。费用按确切的 PaymentDate 分组,而不是按月和年分组——这是我需要做的。如果我在 SELECT 子句中删除 Datename 和 year 或者我只按月或年分组,这个问题仍然存在。除 DATEPART()、month() 或 year() 之外的分组工作正常。也许这是我错过的简单事情?有时我只是瞎了眼。
在 SQL Server Management Studio / SQL Server 2012 中遇到。