【问题标题】:sql query group by with aggregatesql查询分组与聚合
【发布时间】:2017-08-20 07:29:45
【问题描述】:

以下表为例:

    zipcode |  zipsource | patientcount
    -----------------------------------
     81501  |     CMHSP  |   10
     81503  |     CMHSP  |   20
     81505  |     CMHSP  |   30
     81501  |     SMHRMC |   15
     81503  |     SMHRMC |   25
     81505  |     SMHRMC |   35

对于 Source 和 Source = SMHRMC(通常是一个参数,但在示例中我选择了 SMHRMC),尝试仅显示患者人数超过总数 20% 的邮政编码。输出表如下:

   zipcode  | zipsource  |  patientcount | Total  | Percent
   --------------------------------------------------------
    81503   |  SMHRMC    |   25          |  75    |  25%
    81505   |  SMHRMC    |   35          |  75    |  47%

我已经尝试了多个查询,但目前我认为我还没有接近。有任何想法吗?

有效的查询如下:

    select zipcode, 
           zip_source, 
           patient_count, 
           total_count, 
           patient_count *100/total_count as percentage
    from Zip_Count_Source
    cross join (select sum(patient_count) as total_count
                from zip_count_Source
                where zip_source = 'COMHSP') as X
    where zip_source = 'COMHSP' and  patient_count*100/total_count > 1

但是我现在遇到的问题是 Zip_source 可以是一个多值参数,所以我将子句更改为 zip_source in ('COMHSP', 'SMHRMC') 并且它可以工作,但我想为每个来源计算 total_count但不是两个来源的组合。 Group By 在 where 子句之后不起作用。感谢所有帮助。

【问题讨论】:

    标签: mysql count group-by sum aggregate


    【解决方案1】:

    试试这个:

    SELECT zipcode, zipsource, patientcount * 100 / total_count
    FROM mytable
    CROSS JOIN (SELECT SUM(patientcount) AS total_count
                FROM mytable
                WHERE zipsource = 'SMHRMC') AS x
    WHERE zipsource = 'SMHRMC' AND patientcount / total_count > 0.2
    

    查询使用CROSS JOIN 以将patientcount 的总数与表相关联。使用此计数,我们可以计算百分比,并过滤掉任何不超过所需值的行。

    Demo here

    【讨论】:

      【解决方案2】:

      这应该可以解决问题

      select  t1.zipcode,
              t1.zipsource,
              t1.patientcount,
              t2.total,
              t1.patientcount / t2.total * 100 as percent
      from    yourTable t1
      join    (
                  select  zipcode, sum(patientcount) as total
                  from    yourTable
                  group by zipcode
              ) t2
      on      t1.zipcode = t2.zipcode
      where   t1.patientcount / t2.total > 0.2
      

      要过滤单个zipsource,您可以在where 子句中添加条件

      where   t1.patientcount / t2.total > 0.2 and
              t1.zipsource = 'SMHRMC'
      

      【讨论】:

        【解决方案3】:

        当连接到另一个表时,通常有一个连接条件来过滤掉阻止笛卡尔积(一个表的内容乘以另一个表的内容)的行。由于派生表(总计)返回单个值,因此不需要连接条件(table1 的内容乘以 1 = table1)。

        只要该值代表您想要表达的内容,这是可以接受的(即,删除 where 条件,它将产生所有患者的总数)。

        select zipcode,
               zipsource,
               sum(patientcount) as patientcount,
               Total,
               concat(round(100*sum_patientcount/Total),'%') as `SHRMC_%`
          from table1,
               (select count(*) as Total
                  from table1
                 where zipsource='SHRMC') as total
         where zipsource='SHRMC'
         group by zipcode
           having sum(patientcount)/total >= .2
        

        【讨论】:

        • 这产生了错误 mytable.zip_source 在选择列表中无效,因为它不包含在聚合函数或 GroupBy 子句中。当我在那里有 HAVING 子句时,它产生了同样的错误。感谢您的答复。下面的查询确实有效。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-19
        • 1970-01-01
        相关资源
        最近更新 更多