groupby和aggregate函数不在一起组合(同一组下的多个聚合数)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了groupby和aggregate函数不在一起组合(同一组下的多个聚合数)相关的知识,希望对你有一定的参考价值。

我有两个表,我想分组并获得总价值的总和

select t.originfacilitycode, sum(t.cnt_total),t.soldto,
  case when t.product_id in ('81','36','76','384') then 'Expedited'
   when t.product_id in ('77','82','83','383') then 'Ground'
   when t.product_id in('631') then 'Max' end as parcel_category
    from ops_owner.volume_summary_month t, 
  ops_owner.account_data_pickup p 
 where t.soldto = p.soldto
 and t.product_id in ('81','36','76','384','77','82','83','383','631')
and  t.year = '2017'
and t.month in ('11','12')
   and t.soldto  = '5112087'


group by p.customer_name, t.originfacilitycode,t.soldto,
  case when t.product_id in ('81','36','76','384') then 'Expedited'
   when t.product_id in ('77','82','83','383') then 'Ground'
   when t.product_id in('631') then 'Max' end 

having sum(t.cnt_total) > 60

但是,结果并非按组分组唯一。

ORIGINFACILITYCODE  SUM(T.CNT_TOTAL)    SOLDTO  PARCEL_CATEGORY
USEWR1                  156864          5112087 Expedited
USEWR1                   78432          5112087 Expedited

如果我按ORIGINFACILITYCODE分组,SOLDTO,PARCEL_CATEGORY为什么我们得到多次返回,应该分组,对吧?

ops_owner.account_data_pickup有重复如下,但它应该选择第一个,对吧?

 SOLDTO      PICKUP CUSTOMER_NAME
 5112087    5314711 GOGOTECH
 5112087    5320536 GOGOTECH II, LLC

ops_owner.volume_summary_month已将sold_to列为列


更新

我为同一个帐户提取量

select t.originfacilitycode, sum(t.cnt_total) 
from ops_owner.volume_summary_month t
where t.soldto = '5112087'
and  t.year = '2017'
and t.month in ('11','12')

group by t.originfacilitycode


ORIGINFACILITYCODE  SUM(T.CNT_TOTAL)
USATL1                   1
USEWR1                  78432
USDFW1                   1

有人有任何线索吗?为什么我的第一个查询有第一行?

ORIGINFACILITYCODE  SUM(T.CNT_TOTAL)    SOLDTO  PARCEL_CATEGORY
USEWR1                  156864          5112087 Expedited
答案

看着这个查询

SELECT
    T.originfacilitycode,
    T.cnt_total,
    T.soldto,
    CASE 
        WHEN T.product_id IN ('81','36','76','384') THEN 'Expedited'
        WHEN T.product_id IN ('77','82','83','383') THEN 'Ground'
        WHEN T.product_id IN('631') THEN 'Max'
    END AS parcel_category
FROM
    ops_owner.volume_summary_month T,
    ops_owner.account_data_pickup P 
WHERE
    T.soldto = P.soldto
    AND T.product_id IN ('81','36','76','384','77','82','83','383','631')
    AND T.YEAR = '2017'
    AND T.MONTH IN ('11','12')
    AND T.soldto  = '5112087'
GROUP BY
    P.customer_name,
    T.originfacilitycode,
    T.soldto,
    CASE 
        WHEN T.product_id IN ('81','36','76','384') THEN 'Expedited'
        WHEN T.product_id IN ('77','82','83','383') THEN 'Ground'
        WHEN T.product_id IN('631') THEN 'Max'
    END AS parcel_category
HAVING SUM(T.cnt_total) > 60

正如您已经评论过的那样,P.customer_name上有多个不同的值,因此这是重复行的原因。

您必须在此处选择所需的行为:

  • 通过或删除组中的t.cnt_total,从所有可能的值中取P.customer_name
  • P.customer_name上有多个值时,定义另一个行为

以上是关于groupby和aggregate函数不在一起组合(同一组下的多个聚合数)的主要内容,如果未能解决你的问题,请参考以下文章

类之间的关系详解:Aggregation(聚合)Composition(组合)Dependency(依赖)Generalization(泛化)Realization(实现)

类之间的关系详解:Aggregation(聚合)Composition(组合)Dependency(依赖)Generalization(泛化)Realization(实现)

MySQL 聚合函数聚合(组合)函数概述

提高性能(矢量化?) pandas.groupby.aggregate

pandas使用groupby函数进行分组聚合并使用agg函数将每个分组特定变量对应的多个内容组合到一起输出(merging content within a specific column of g

pandas使用groupby函数进行分组聚合使用agg函数指定聚合统计计算的数值变量并自定义统计计算结果的名称(naming columns after aggregation)