Skip to content
Advertisement

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

I may have asked this question incorrectly but I have the following query where I’m trying to pull 3 different metrics for a given store. When I pull them though it comes out as below:

enter image description here

Is there a way I could get all of these so that the Generated, Post, and Daily Percentages are all on one line? I would want it to look something like this

enter image description here

This is the query I used for it:

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

Advertisement

Answer

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

If you want the value of SE_PCNT_Generated, SE_PCNT_At_Post and SE_PCNT_Daily store wise and report date wise then use :

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
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement