由DATEPART分组()错误/错误

时间:2015-04-07 12:48:02

标签: sql tsql group-by datepart

我遇到了一个由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中遇到。

2 个答案:

答案 0 :(得分:0)

您没有使用SELECT子句中提供的GROUP BY子句字段。请尝试以下方法:

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, DATENAME(month,month(cplanit.PaymentDate)), year(cplanit.PaymentDate)

无论如何它适用于所有情况:

CREATE TABLE #Tempas
(
   Name NVARCHAR(80),
   contractID NVARCHAR(20),
   Data DATETIME2
)
INSERT INTO #Tempas VALUES('Adobe CLP Agreement - Initial purchase' ,'1','20010101 10:10:10')
INSERT INTO #Tempas VALUES('Adobe CLP Agreement - Initial purchase' ,'1','20010101 10:10:10')
INSERT INTO #Tempas VALUES('Adobe CLP Agreement - Initial purchase' ,'1','20010101 10:10:10')
INSERT INTO #Tempas VALUES('Adobe CLP Agreement - Initial purchase' ,'1','20010101 10:10:10')
INSERT INTO #Tempas VALUES('Adobe CLP Agreement - Initial purchase' ,'1','20020101 10:10:10')
INSERT INTO #Tempas VALUES('Adobe CLP Agreement - Initial purchase' ,'1','20020101 10:10:10')
INSERT INTO #Tempas VALUES('Adobe CLP Agreement - Initial purchase' ,'1','20020101 10:10:10')
INSERT INTO #Tempas VALUES('Adobe CLP Agreement - Initial purchase' ,'1','20020101 10:10:10')

SELECT Name, contractID, DATENAME(month,month(data)) AS Month,  year(data) AS year 
FROM #Tempas 
GROUP BY Name, contractID,  DATEPART(month,data), DATEPART(year,data)

DROP TABLE #tempas

<强>输出:

              Name                contractID Month  year
Adobe CLP Agreement - Initial purchase  1   January 2001
Adobe CLP Agreement - Initial purchase  1   January 2002

使用相同的值并跟随查询:

SELECT Name, contractID, DATENAME(month,month(data)) AS Month,  year(data) AS year 
FROM #Tempas 
GROUP BY Name, contractID,  DATENAME(month,month(data)), year(data)

输出相同:

              Name                contractID Month  year
Adobe CLP Agreement - Initial purchase  1   January 2001
Adobe CLP Agreement - Initial purchase  1   January 2002

答案 1 :(得分:0)

即使使用当前的group by子句,代码也可以正常工作。问题可能出在查询的另一部分吗?

declare @ContractTable table (
Name nvarchar(20),
ContractId int,
Cost Decimal,
PaymentDate DateTime)

insert into @ContractTable values ('Name', 1, 100, N'20150101')
insert into @ContractTable values ('Name', 2, 100, N'20150101')
insert into @ContractTable values ('Name', 1, 100, N'20150102')
insert into @ContractTable values ('Name', 1, 100, N'20150102')
insert into @ContractTable values ('Name', 2, 100, N'20150104')
insert into @ContractTable values ('Name', 2, 100, N'20150104')
insert into @ContractTable values ('Name', 2, 100, N'20150105')
insert into @ContractTable values ('Name', 2, 100, N'20150105')
insert into @ContractTable values ('Name', 2, 100, N'20150105')
insert into @ContractTable values ('Name', 2, 100, N'20150105')

SELECT 
     Name
    ,contractID
    ,Sum(Cost) as costs
    ,DATENAME(month,month(PaymentDate)) as [month]
    ,year(PaymentDate) as [year]
FROM 
    @ContractTable
Group By 
     Name
    ,contractID
    ,DATEPART(month,PaymentDate)
    ,DATEPART(year, PaymentDate)