【问题标题】:Group by DATEPART() error/mistake按 DATEPART() 错误/错误分组
【发布时间】:2015-04-07 12:48:02
【问题描述】:

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

【问题讨论】:

  • 另外:为什么 group by 子句与 SELECT 字段不同?你试过了吗:GROUP BY con.Name, con.contractID, DATENAME(month,month(cplanit.PaymentDate)), year(cplanit.PaymentDate)
  • "费用按确切的 PaymentDate 分组" 我必须在这里更正自己,每月只有一个 PaymentDate,与我的想法相反。对不起我的帖子,我得到了解决方案:连接只产生了多行。因此,“不同”解决了它。无论如何,非常感谢,stackoverflow 多次证明对我非常有用:)
  • @TedOckham 。 . .考虑删除问题。
  • @Gordon Linoff “我们不建议删除带有答案的问题,因为这样做会剥夺未来读者的知识。重复删除已回答的问题可能会导致您的帐户被阻止提问。您确定要要删除吗?” 好吧,这段文字吓到我不敢删除它^^

标签: sql tsql group-by datepart


【解决方案1】:

您不提供 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

【讨论】:

    【解决方案2】:

    即使使用当前的 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 2015-11-27
      • 2014-07-13
      相关资源
      最近更新 更多