簡體   English   中英

如何計算一年中每個月按一個字段分組的mysql記錄

[英]how to count mysql records for each month of the year grouped by one field

我一直在審查一些實例,但沒有找到我需要的。 該查詢顯示按月分組的每個代理商的記錄數。 這是我的表結構的一部分:

recid | agency_id | departure_date 

因此,我需要按代理商和月份(“ department_date”的月份)來計算“收據”組,並獲得總

  Agency_id  | JAN | FEB | MAR | APR | MAY | ........ | TOTAL 

      10        100  80    100   120   100    1200   

看起來很容易。 但我找不到解決辦法。 任何幫助將不勝感激!

嘗試

SELECT agency_id, 
       SUM(CASE WHEN MONTH(departure_date) = 1 THEN 1 ELSE 0 END) Jan,
       SUM(CASE WHEN MONTH(departure_date) = 2 THEN 1 ELSE 0 END) Feb,
       SUM(CASE WHEN MONTH(departure_date) = 3 THEN 1 ELSE 0 END) Mar,
       ...
       COUNT(*) Total
  FROM table1
 WHERE departure_date BETWEEN '2013-01-01' AND '2013-12-31'
 GROUP BY agency_id

輸出:

| AGENCY_ID | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC | TOTAL |
---------------------------------------------------------------------------------------------
|         1 |   1 |   1 |   1 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |     3 |
|         2 |   2 |   2 |   1 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |     5 |
|         3 |   0 |   0 |   2 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |     2 |

SQLFiddle

嘗試

 select agency_id, 
    sum(if(month(`departure_date`) = 1,1,0)) as 'JAN',
    sum(if(month(`departure_date`) = 2,1,0)) as 'FEB',
    sum(if(month(`departure_date`) = 3,1,0)) as 'MAR',
    sum(if(month(`departure_date`) = 4,1,0)) as 'APR',
    sum(if(month(`departure_date`) = 5,1,0)) as 'MAY',
    sum(if(month(`departure_date`) = 6,1,0)) as 'JUN',
    sum(if(month(`departure_date`) = 7,1,0)) as 'JULY',
    sum(if(month(`departure_date`) = 8,1,0)) as 'AUG',
    sum(if(month(`departure_date`) = 9,1,0)) as 'SEPT',
    sum(if(month(`departure_date`) = 10,1,0)) as 'OCT',
    sum(if(month(`departure_date`) = 11,1,0)) as 'NOV',
    sum(if(month(`departure_date`) = 12,1,0)) as 'DEC',
            count(agency_id) as TOTAL 
 from tablename
 where .....
 group by agency_id

試試看:

SELECT 
    agency_id,
    SUM(IF(month = 1, numRecords, NULL)) AS 'January',
    SUM(IF(month = 2, numRecords, NULL)) AS 'Feburary',
    SUM(IF(month = 3, numRecords, NULL)) AS 'March',
    SUM(IF(month = 4, numRecords, NULL)) AS 'April',
    SUM(IF(month = 5, numRecords, NULL)) AS 'May',
    SUM(IF(month = 6, numRecords, NULL)) AS 'June',
    SUM(IF(month = 7, numRecords, NULL)) AS 'July',
    SUM(IF(month = 8, numRecords, NULL)) AS 'August',
    SUM(IF(month = 9, numRecords, NULL)) AS 'September',
    SUM(IF(month = 10, numRecords, NULL)) AS 'October',
    SUM(IF(month = 11, numRecords, NULL)) AS 'November',
    SUM(IF(month = 12, numRecords, NULL)) AS 'December',
    SUM(numRecords) AS total
    FROM (
        SELECT agency_id, month(departure_date) AS month, count(*) as numRecords
        FROM your_table_name
        GROUP BY agency_id, month
    ) AS SubTable1 GROUP BY agency_id  

對於特定的agency_id沒有記錄的月份,此查詢將顯示null 如果要將其顯示為0 ,請更新查詢並將NULL替換為文字0

看看帶有ROLLUP子句的解決方案,它顯示了您所需要的,但是給出了另一個輸出-

SELECT
  agency_id, MONTH(departure_date) month, COUNT(*) count
FROM
  sales
GROUP BY
  agency_id, MONTH(departure_date) WITH ROLLUP
WHERE
  YEAR(departure_date) = 2013

暫無
暫無

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

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