【问题标题】:MySQL get COUNT and SUM on different GROUP BY in one SELECTMySQL 在一次 SELECT 中获取不同 GROUP BY 的 COUNT 和 SUM
【发布时间】:2014-07-31 14:13:47
【问题描述】:

我需要从表中选择与此相关的数据:

monthStart  monthEnd newPhones totalPhones oblid
1           1        1         2           1 
2           2        1         2           2
1           2        2         2           3
2           2        1         1           4
2           3        0         3           5

所以我想选择 4 个字段:月份,以 monthStart 为基础计算月份的 obj,以 monthEnd 为基础的 newPhones 总和,以 monthEnd 为基础的 totalPhones 总和。

所以对于这个数据我需要选择这个:

month  count  totalPhones newPhones
1      3      2           1
2      2      5           4
3      0      3           0

第 1 个月的计数 = 3,因为我们有 3 行,monthStart = 1,但我们只有一行 monthEnd = 1,所以 1 个月的电话总数 = 2,newPhones = 1 计算第 3 个月 = 0,因为我们有 0 行,monthStart = 3, 但是我们有 3 个 totalPhones 和 0 个 newPhones for monthEnd = 3 - 我们应该显示这些数据。

我一直坚持这一点。我试图从这个选择的结果中进行选择:

SELECT
monthStart,
monthEnd,
count(1) as uploaded,
sum(newPhones) as newPhones,
sum(totalPhones) as totalPhones
from TestGB
group by monthEnd, monthStart

但我无法得到理想的结果。 感谢您的帮助!

【问题讨论】:

    标签: mysql sql group-by


    【解决方案1】:

    因此,如果有足够的数据可以代表所有月份,我认为 StevieG 的答案有效。对于像给定样本数据这样的较小数据集,其中第 3 个月在 monthEnd 但不在 monthStart 中,那么就会出现问题。然后你需要一些东西来确保所有月份都被表示出来,我用 c 做的,合并只是为了让事情变得漂亮。

    SELECT 
      c.month,
      coalesce(a.mycount,0),
      coalesce(b.totalPhones,0),
      coalesce(b.newphones,0)
    FROM
      (SELECT monthStart as month FROM TestGB
       UNION
       SELECT monthEnd as month FROM TestGB) c 
    LEFT OUTER JOIN 
      (SELECT
       monthStart as month,
       count(distinct obild) as mycount,
       from TestGB
       group by monthStart) a on a.month = c.month
    LEFT OUTER JOIN 
      (SELECT
       monthStart as month,
       sum(newPhones) as newPhones,
       sum(totalPhones) as totalPhones
       from TestGB
       group by monthEnd) b ON b.month = c.month
    

    【讨论】:

    • 效果很好!只有我必须将monthStart as month 更改为monthEnd as month,因为该数字与monthEnd 有关。所以这很好用!你能告诉我 - 我可以在没有子选择和联合的情况下做到这一点吗?
    • 啊,是的,感谢您了解这一点。不幸的是,如果没有子选择和联合,我认为您无法做到这一点。因为您按 2 个不同的列进行分组,所以您必须在不同的(子)查询中执行这些操作,而带有 union 的子查询只是因为 MySQL 不进行完全外连接,否则我们可以在 a 和b 并跳过 c。
    【解决方案2】:

    也许是这样的......

    CREATE TABLE #monthstartdata
    (
        themonth INT,
        themonthcount int
    )
    
    CREATE TABLE #monthenddata
    (
        themonth INT,
        totalphones INT,
        newphones INT,
    )
    
    INSERT INTO #monthstartdata
                ( themonth, themonthcount )
    SELECT monthStart, COUNT(#monthstart) FROM TestGB
    GROUP BY monthStart
    
    INSERT INTO #monthenddata
            ( themonth, totalphones, newphones )
    SELECT monthEnd, COUNT(totalphones), COUNT(newPhones) FROM TestGB
    GROUP BY monthEnd
    
    SELECT #monthstartdata.themonth, themonthcount, totalphones, newphones FROM #monthstartdata
    INNER JOIN #monthenddata ON #monthstartdata.themonth = #monthenddata.themonth
    

    【讨论】:

      【解决方案3】:

      假设我已经正确理解了这个问题,我怀疑你想用子查询来做这个。像这样的事情:

      SELECT 
        IFNULL(a.month,b.month)
        a.mycount,
        b.totalPhones,
        b.newphones
      FROM
        (SELECT
         monthStart as month,
         count(distinct obild) as mycount,
         from TestGB
         group by monthStart) a
      LEFT OUTER JOIN 
        (SELECT
         monthStart as month,
         sum(newPhones) as newPhones,
         sum(totalPhones) as totalPhones
         from TestGB
         group by monthEnd) b ON a.month = b.month
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-14
        • 1970-01-01
        • 2020-08-15
        • 2021-02-09
        • 1970-01-01
        相关资源
        最近更新 更多