POSTGRES Group BY中的聚合函数

时间:2018-01-22 11:53:52

标签: sql postgresql group-by

我有以下Postgres代码,我希望Group BY "Personal CC",但由于显而易见的原因,我无法对聚合字段执行此操作。

如果我没有放Group BY,那么我会收到错误的"Number of Assistant Supervisors in Same Month" Personal CC

请帮我详细说明如何计算金额。是的,我做了一些研究,并且知道我们可以使用INNER JOIN,但我失败了。

  1. 期望的结果应该是降序中的个人CC总和
  2. 总和应仅适用于我在下面给出的月份和年份,但是 它需要整年的价值观是错误的
  3. 如果没有,我只需要将个人CC的总和包括在内 请给我们一个解决方案,如何通过不包括来获得总和 它在Group BY
  4. 我没有阅读文档,但我一直在研究 IT
  5. 对于每个赞助商,我有各个国家的每月cc。个人CC值。每个月。我想总结它们以获得一个独特的总结值

     SELECT 
            extended_downline."Sponsor FBO ID"
            ,extended_downline."Sponsor Name"
            ,mcc.processing_month__c AS "Processing Month"
            ,mcc.processing_year__c AS "Processing Year"
    
            ,sum(mcc.personal_cc_mtd__c) AS "Personal CC"
            ,sum("Same Month") as "Number of Assistant Supervisors in Same Month"
            ,bool_or(mcc.current_month_active__c) AS "Current Month Active"
    
        FROM extended_downline
        LEFT OUTER JOIN connect.monthly_cc__c mcc ON mcc.distributor__c = extended_downline.sponsor_distributor__c
            AND mcc.op_company__c = extended_downline."Operating Company"
            AND (
                (
                    mcc.processing_year__c = '2017'
                    AND mcc.processing_month__c = 10
                    )
                )
        WHERE extended_downline."Same Month" <> 0
            AND extended_downline."Generation" <> 0
            AND mcc.personal_cc_mtd__c IS NOT NULL
        GROUP BY
            extended_downline."Sponsor FBO ID"
            ,extended_downline."Sponsor Name"    
            ,mcc.processing_year__c
            ,mcc.processing_month__c
            ,mcc.personal_cc_mtd__c
            ,extended_downline."Same Month"
    
        ORDER BY "Personal CC" DESC LIMIT 20
    

1 个答案:

答案 0 :(得分:0)

您可以使用Order By ColumnPosition

例如:

Order By 4 desc