簡體   English   中英

在一個列上分組並獲得該列的總和

[英]group by on a column and get the sum of that column

我有一張桌子:

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

該表中有很多記錄。 我必須在特定年份的每個月的第一天獲取每個dagName的mailboxcount的總和。

------------------------------------
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

如果我的查詢附帶一些額外的列,我很好。 但這並沒有給我正確的結果。 有幫助嗎??

我可能會完全誤解這個問題,但是以下內容是否足夠?

  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)
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))

試試這個-

查詢:

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
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查詢,此查詢返回按年份和月份計數的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM