按 DATEPART() 分组 error/mistake
Group by DATEPART() error/mistake
我遇到了 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
相同的条目。成本按确切的付款日期分组,而不是按月和年分组——这是我需要做的。如果我在 SELECT 子句中删除 Datename 和 year,或者如果我只按月或年分组,这个问题仍然存在。除 DATEPART()、month() 或 year() 之外的分组工作正常。也许这是我错过的简单事情?有时我只是瞎了眼。
在 SQL Server Management Studio / SQL Server 2012 中遇到。
您不能在 GROUP BY 子句中提供在 SELECT 子句中提供的字段。尝试以下操作:
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
即使使用当前的 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)
我遇到了 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
相同的条目。成本按确切的付款日期分组,而不是按月和年分组——这是我需要做的。如果我在 SELECT 子句中删除 Datename 和 year,或者如果我只按月或年分组,这个问题仍然存在。除 DATEPART()、month() 或 year() 之外的分组工作正常。也许这是我错过的简单事情?有时我只是瞎了眼。
在 SQL Server Management Studio / SQL Server 2012 中遇到。
您不能在 GROUP BY 子句中提供在 SELECT 子句中提供的字段。尝试以下操作:
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
即使使用当前的 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)