简体   繁体   中英

Group by DATEPART() error/mistake

I encountered a Group by DATEPART() clause Problem. My code is

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)

I'm getting this:

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

Identical entries. Costs are grouped by the exact PaymentDate, rather than month and year - which is what I need to do. This problem stays if I remove Datename and year in the SELECT clause or if I group by month or year only. Grouping except for DATEPART(), month() or year() to group for works fine. Maybe it's something simple I missed? Sometimes I'm just blind.

Encountered in SQL Server Management Studio / SQL Server 2012.

You not privide in GROUP BY clause fields which are provided in SELECT clause. Try following:

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)

Anyway It working in all cases:

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

OUTPUT:

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

With the same values and following query:

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)

OUTPUT same:

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

The code seems to work fine as it is, even with the current group by clause. Could the issue be in another part of your query ?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM