我是否需要执行 Join 才能合并其中包含 0 的多列

Do I need to perform a Join in order to merge multiple columns with 0 in them

我可能问错了这个问题,但我有以下查询,我试图为给定的商店提取 3 个不同的指标。当我拉出它们时,结果如下:

有没有一种方法可以获得所有这些,以便生成的百分比、Post 和每日百分比都在一条线上?我希望它看起来像这样

这是我使用的查询:

SELECT
                        STORE_NUM,                  
                        --REPORT_TYPE,      
                        REPORT_STARTDATE,       
                        --GEN_IMPORT_DT,        
                        --POST_IMPORT_DT,       
                        --SID,      
                        --REQ,
                    
                        CASE WHEN REPORT_TYPE = 'G' THEN SE_PCNT ELSE 0 END AS SE_PCNT_Generated,
                        CASE WHEN REPORT_TYPE = 'A' THEN SE_PCNT ELSE 0 END AS SE_PCNT_At_Post,
                        CASE WHEN REPORT_TYPE = 'D' THEN SE_PCNT ELSE 0 END AS SE_PCNT_Daily
            FROM (
            
            SELECT  STORE_NUM,                  
                    REPORT_TYPE,        
                    REPORT_STARTDATE,       
                    DATE_IMPORT_2 AS GEN_IMPORT_DT,     
                    DATE_IMPORT_3 AS POST_IMPORT_DT,        
                    SUM (COVERAGE_EFFECTIVE_SCHED_DEMAND) AS SID,       
                    SUM (COVERAGE_EFFECTIVE_REQUIRED) AS REQ,       
                    CASE WHEN SUM (COVERAGE_EFFECTIVE_SCHED_DEMAND)>0 THEN      
                            SUM (COVERAGE_EFFECTIVE_SCHED_DEMAND) / SUM(COVERAGE_EFFECTIVE_REQUIRED) ELSE 0 END AS SE_PCNT                      
            FROM DNA_DM..STOREOPS_SCHEDULE_EFFECTIVENESS_RPT                
            WHERE
            --REPORT_TYPE = 'A' AND REC_TYPE = 'S'              
            REPORT_STARTDATE >='03/08/21'
            AND STORE_NUM = 2
            GROUP BY 1, 2, 3, 4, 5  
            
            ) AS q

只需在您的每个 CASE WHEN 周围添加一个 SUM(..),并在 q

之后添加一个 GROUP BY STORE_NUM, REPORT_STARTDATE

ps;您不需要 ELSE 0 - 如果您省略它,那么当前带有 0 的行将改为 NULL; NULL 不参与聚合,因此当分组依据执行其“将 3 行压缩为 1”的工作时,使用 MAX 之类的东西而不是 SUM 来“获取列中唯一的非空值”变得更容易

因此,对于未来,我推荐更通用的模式:

SELECT 
  x, y,
  MAX(CASE WHEN ... THEN ... END) as ... --do not use ELSE here!
FROM
  ...
GROUP BY
  x, y

这种查询形式适用于 CASE WHEN 选择字符串、日期或其他无法求和的情况(并且适用于像您这样的情况,如果它只是一个数值一列空值) - 总而言之,它比使用 SUM(CASE WHEN ... THEN ... ELSE 0 END)

更灵活
SELECT
                        STORE_NUM,                  
                        max(REPORT_STARTDATE),                               
                        max(CASE WHEN REPORT_TYPE = 'G' THEN SE_PCNT ELSE 0 END )AS SE_PCNT_Generated,
                        max(CASE WHEN REPORT_TYPE = 'A' THEN SE_PCNT ELSE 0 END )AS SE_PCNT_At_Post,
                        max(CASE WHEN REPORT_TYPE = 'D' THEN SE_PCNT ELSE 0 END )AS SE_PCNT_Daily
            FROM (
            
            SELECT  STORE_NUM,                  
                    REPORT_TYPE,        
                    REPORT_STARTDATE,       
                    DATE_IMPORT_2 AS GEN_IMPORT_DT,     
                    DATE_IMPORT_3 AS POST_IMPORT_DT,        
                    SUM (COVERAGE_EFFECTIVE_SCHED_DEMAND) AS SID,       
                    SUM (COVERAGE_EFFECTIVE_REQUIRED) AS REQ,       
                    CASE WHEN SUM (COVERAGE_EFFECTIVE_SCHED_DEMAND)>0 THEN      
                            SUM (COVERAGE_EFFECTIVE_SCHED_DEMAND) / SUM(COVERAGE_EFFECTIVE_REQUIRED) ELSE 0 END AS SE_PCNT                      
            FROM DNA_DM..STOREOPS_SCHEDULE_EFFECTIVENESS_RPT                
            WHERE
            --REPORT_TYPE = 'A' AND REC_TYPE = 'S'              
            REPORT_STARTDATE >='03/08/21'
            AND STORE_NUM = 2
            GROUP BY 1, 2, 3, 4, 5  
            
            ) AS q
group by store_num

如果您想要 SE_PCNT_Generated、SE_PCNT_At_Post 和 SE_PCNT_Daily 的值明智地存储并报告日期,那么使用:

SELECT
                            STORE_NUM,                  
                            REPORT_STARTDATE,                               
                            max(CASE WHEN REPORT_TYPE = 'G' THEN SE_PCNT ELSE 0 END )AS SE_PCNT_Generated,
                            max(CASE WHEN REPORT_TYPE = 'A' THEN SE_PCNT ELSE 0 END )AS SE_PCNT_At_Post,
                            max(CASE WHEN REPORT_TYPE = 'D' THEN SE_PCNT ELSE 0 END )AS SE_PCNT_Daily
                FROM (
                
                SELECT  STORE_NUM,                  
                        REPORT_TYPE,        
                        REPORT_STARTDATE,       
                        DATE_IMPORT_2 AS GEN_IMPORT_DT,     
                        DATE_IMPORT_3 AS POST_IMPORT_DT,        
                        SUM (COVERAGE_EFFECTIVE_SCHED_DEMAND) AS SID,       
                        SUM (COVERAGE_EFFECTIVE_REQUIRED) AS REQ,       
                        CASE WHEN SUM (COVERAGE_EFFECTIVE_SCHED_DEMAND)>0 THEN      
                                SUM (COVERAGE_EFFECTIVE_SCHED_DEMAND) / SUM(COVERAGE_EFFECTIVE_REQUIRED) ELSE 0 END AS SE_PCNT                      
                FROM DNA_DM..STOREOPS_SCHEDULE_EFFECTIVENESS_RPT                
                WHERE
                --REPORT_TYPE = 'A' AND REC_TYPE = 'S'              
                REPORT_STARTDATE >='03/08/21'
                AND STORE_NUM = 2
                GROUP BY 1, 2, 3, 4, 5  
                
                ) AS q
    group by store_num,REPORT_STARTDATE