【问题标题】:group by on a column and get the sum of that column按列分组并获得该列的总和
【发布时间】:2013-05-08 09:18:52
【问题描述】:

我有一张桌子:

------------------------------------
ReportDate  | DAGName | MailboxCount
------------------------------------

这张表有很多记录。我必须在特定年份的每个月的第一天获取每个 dagName 的邮箱总数。

------------------------------------
ReportDate  | DAGName | MailboxCount
------------------------------------
01-01-2012  | abc     |  25
01-02-2012  | xyz     |  55
01-02-2012  | abc     |  125
01-03-2012  | lmn     |  225
01-01-2012  | ghf     |  325
01-03-2012  | kil     |  525
11-03-2012  | kil     |  525
21-03-2012  | kil     |  625
10-05-2012  | jil     |  225
20-11-2012  | kil     |  1525
04-03-2012  | Mil     |  5025

所以我想要的结果是

---------------------------------
Month Name     | Count
---------------------------------
 January       | 350
 Ferbuary      | 150
 March         | 850 

我的查询

SELECT SUM(MailboxCount) AS Count,DagName
              ,MONTH(CAST(ReportDate AS DATETIME))
              ,DATENAME(month, ReportDate)  AS 'Month Name'
        FROM MailboxDatabase
        WHERE (CONVERT(VARCHAR, CONVERT(VARCHAR(10), [ReportDate], 103), 103)
        IN ( '01/01/'+ @year,'01/02/'+ @year,
             '01/03/'+ @year,'01/04/'+ @year,
             '01/05/'+ @year,'01/06/'+ @year,
             '01/07/'+ @year,'01/08/'+ @year,
             '01/09/'+ @year,'01/10/'+ @year,
             '01/11/'+ @year,'01/12/'+ @year 
            ))
        GROUP BY  MONTH(CAST(ReportDate AS DATETIME)),DATENAME(month, ReportDate),DagName  
        ORDER BY 2

如果我的查询附带一些额外的列,我很好。但这并没有给我正确的结果。有什么帮助吗??

【问题讨论】:

  • 删除 DagName 它不属于您的分组,reportDate 是什么类型,不是日期时间
  • 永远不要在没有指定长度的情况下使用VARCHAR,否则它会回来困扰你。
  • 不,不是因为日期不是一个月的第一天......

标签: sql sql-server group-by


【解决方案1】:

我可能完全误解了这个问题,但以下内容还不够吗?

  SELECT  [Month Name] = DATENAME(month, ReportDate), [Count] = SUM(MailboxCount)
  FROM    MailboxDatabase
  WHERE   DAY(ReportDate) = 1
          AND YEAR(ReportDate) = 2012
  GROUP BY
          DATENAME(month, ReportDate)

测试脚本

;WITH MailboxDatabase AS (
  SELECT * FROM (VALUES
      ('01-01-2012', 25)
    , ('02-01-2012', 55)
    , ('02-01-2012', 125)
    , ('03-01-2012', 225)
    , ('01-01-2012', 325)
    , ('03-01-2012', 525)) AS X(ReportDate, MailboxCount)
)
  SELECT  [Month Name] = DATENAME(month, ReportDate)
          , [Count] = SUM(MailboxCount)
          , Month = MONTH(ReportDate)
  FROM    MailboxDatabase
  WHERE   DAY(ReportDate) = 1
          AND YEAR(ReportDate) = 2012
  GROUP BY
          DATENAME(month, ReportDate), MONTH(ReportDate)
  ORDER BY
          MONTH(ReportDate)

【讨论】:

  • 它几乎就在那里,但我需要的是在第一天与 group by of groupsoo
  • @ankur - 我不明白。您能否通过将要求添加到您发布的示例中来澄清(ps。您示例中的计数已关闭)
  • 并非如此,添加这些记录不会改变我发布的声明的结果,但我不确定您是否想要另一个结果。你试过这个说法吗?
  • @ankur - 我用我认为可能的最简单的方式添加了一个订单(通过添加一个月列)。另一种选择是通过(使用 case 语句)添加搜索订单。
【解决方案2】:

试试这个 -

查询:

SET DATEFORMAT dmy

DECLARE @temp TABLE
(
      ReportDate DATETIME
    , DAGName NVARCHAR(50)
    , MailboxCount INT
)

INSERT INTO @temp (ReportDate, DAGName, MailboxCount)
VALUES 
    ('01-01-2012',  'abc', 25),
    ('01-02-2012',  'xyz', 55),
    ('01-02-2012',  'abc', 125),
    ('01-03-2012',  'lmn', 225),
    ('01-01-2012',  'ghf', 325),
    ('01-03-2012',  'kil', 525),
    ('11-03-2012',  'kil', 525),
    ('21-03-2012',  'kil', 625),
    ('10-05-2012',  'jil', 225),
    ('20-11-2012',  'kil', 1525),
    ('04-03-2012',  'Mil', 5025)

DECLARE @year INT = 2012

SELECT 
      [Count] = SUM(MailboxCount)
    , Month_Name = DATENAME(MONTH, ReportDate)
FROM @temp
WHERE DAY(ReportDate) = 01 
    AND YEAR(ReportDate) = @year
GROUP BY ReportDate
ORDER BY ReportDate

输出:

Count       Month_Name
----------- ------------------------------
350         January
180         February
750         March

【讨论】:

    【解决方案3】:
    SELECT DATENAME(MONTH, CONVERT(DATETIME, ReportDate, 105)) Month, 
           SUM(MailboxCount) COUNT
    FROM   tableName
    GROUP  BY DATENAME(MONTH, CONVERT(DATETIME, ReportDate, 105))
    

    更新 1

    SELECT DATENAME(MONTH, CONVERT(DATETIME, ReportDate, 105)) Month, 
           SUM(MailboxCount) COUNT
    FROM   tableName
    WHERE  (CONVERT(VARCHAR(15), CONVERT(DATETIME, ReportDate, 105), 103)
            IN ( '01/01/'+ '2012','01/02/'+ '2012',
                 '01/03/'+ '2012','01/04/'+ '2012',
                 '01/05/'+ '2012','01/06/'+ '2012',
                 '01/07/'+ '2012','01/08/'+ '2012',
                 '01/09/'+ '2012','01/10/'+ '2012',
                 '01/11/'+ '2012','01/12/'+ '2012' 
                ))
    GROUP  BY MONTH(CONVERT(DATETIME, ReportDate, 105)),
              DATENAME(MONTH, CONVERT(DATETIME, ReportDate, 105))
    ORDER  BY MONTH(CONVERT(DATETIME, ReportDate, 105))
    

    【讨论】:

    • 结果是正确的,但是没有日期的条件,因为也会有很多其他日期的记录..
    • 仅供参考,表名为 MailboxDatabase
    • @SenJacob 在演示中真的很重要吗? :)
    • JW 你能在你的演示中加入一些随机日期吗,虽然有相同的 DAG 名称来测试,但它们不是任何一个月的第一天,如果它有效,我可以将它标记为已解决.... .
    • 我的意思是这个 INSERT INTO Tablename ([ReportDate], [DAGName], [MailboxCount]) VALUES ('01-01-2012', 'abc', 25), ('01-02- 2012', 'xyz', 55), ('01-02-2012', 'abc', 125), ('01-03-2012', 'lmn', 225), ('01-01-2012' , 'ghf', 325), ('01-03-2012', 'kil', 525), ('20-11-2012', 'kil', 525), ('11-07-2012', '基尔,525),('31-03-2012','基尔',525),('21-03-2012','基尔',525),('11-03-2012','基尔' , 525);并且无法正常工作
    【解决方案4】:
    SELECT 
          DATENAME(MONTH, CONVERT(DATETIME, ReportDate, 105)) AS [MonthName],
          YEAR(CONVERT(DATETIME, ReportDate, 105)) AS [Year],
          SUM(MailboxCount) COUNT
    FROM tableName
    GROUP BY 
          DATENAME(MONTH, CONVERT(DATETIME, ReportDate, 105)),
          YEAR(CONVERT(DATETIME, ReportDate, 105))
    

    试试上面的 sql 查询,这个查询返回的结果是按年计算的月份和计数。

    【讨论】:

      猜你喜欢
      • 2020-02-10
      • 2019-04-27
      • 2017-03-25
      • 2016-02-20
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 2019-11-26
      • 2021-10-05
      相关资源
      最近更新 更多