左连接

时间:2018-02-04 16:58:16

标签: sql join sas duplicates proc-sql

使用左连接加入invmast& cadv表添加两个新列coupon_pos作为coupon_pos_c和coupon_web作为coupon_web_c,我从右表(cadv)的列中获取重复值。

enter image description here

 /*INVMAST*/
        Proc Sql;
            Create Table INVMAST.INVMAST_01 as
                Select
                        Case
                            When BV_LM = 'Y' Then  (INV_DATE - Day(INV_DATE))
                                            Else INV_DATE
                        END as INV_DATE format=DDMMYY8., BV_LM,
                        /*Case 
                            When INV_TOT NE 0 Then Count(IN_CH) 
                            Else 0
                        End*/ Count(IN_CH) as TOTAL_KIT_NOS, COALESCE(Sum(INV_TOT),0) as TOTAL_KIT_VALUE, 
                        COALESCE(Sum(COUPON_WEB),0) as COUPON_WEB, COALESCE(Sum(COUPON_POS),0) as COUPON_POS,
                        IN_CH, LOCA_CODE, LOCA_NAME, ZONE From INVMAST where IN_CH IN ('N','I') AND CANCL NE 1
                        Group by INV_DATE, BV_LM, IN_CH, LOCA_CODE, LOCA_NAME,ZONE;
        Quit;

/*CADV*/
        PROC SQL;
        CREATE TABLE CADV.CADV_01 AS
        SELECT 
                    Case
                            When BV_LM = 'Y' Then  (INV_DATE - Day(INV_DATE))
                                            Else INV_DATE
                        END as INV_DATE format=DDMMYY8.,COALESCE(SUM(COUPON_POS),0) AS COUPON_POS_C,COALESCE(SUM(COUPON_WEB),0) AS COUPON_WEB_C,
                        BV_LM,IN_CH,LOCA_CODE,LOCA_NAME,ZONE FROM CADV WHERE IN_CH="I" AND CANCL NE 1
                        GROUP BY INV_DATE,BV_LM,IN_CH,LOCA_CODE,LOCA_NAME,ZONE;
                        QUIT;
    /*Joining INVMAST & CADV*/

Proc Sql;
            Create Table INCADV.INCADV_01 as
                Select  a.*,COALESCE(SUM(b.COUPON_POS_C),0) as COUPON_POS_C,COALESCE(SUM(b.COUPON_WEB_C),0) AS COUPON_WEB_C
                FROM INVMAST.INVMAST_01 AS a LEFT JOIN CADV.CADV_01 as b
                                On a.INV_DATE = b.INV_DATE 
                                Group by a.INV_DATE, a.LOCA_CODE, a.LOCA_NAME, a.ZONE,a.BV_LM,a.IN_CH;
        Quit;

2 个答案:

答案 0 :(得分:1)

每次在inv_date上匹配时,您都会从右侧表中复制聚合值。尝试向join语句添加条件,例如

/*Joining INVMAST & CADV*/
Create Table INCADV.INCADV_01 as
Select  a.*
    , COALESCE(SUM(b.COUPON_POS_C),0) as COUPON_POS_C
    , COALESCE(SUM(b.COUPON_WEB_C),0) AS COUPON_WEB_C
FROM INVMAST.INVMAST_01 AS a 
LEFT JOIN CADV.CADV_01 as b
    On a.INV_DATE = b.INV_DATE
    /* Add additional joins here */
    and a.BV_LM = b.BV_LM
    and a.IN_CH = b.IN_CH
    and a.LOCA_CODE = b.LOCA_CODE
    and a.LOCA_NAME = b.LOCA_NAME
    and a.zone = b.zone
Group by a.INV_DATE, a.LOCA_CODE, a.LOCA_NAME, a.ZONE, a.BV_LM, a.IN_CH;
Quit;

答案 1 :(得分:0)

您的查询使得右侧表格按

进行分组
INV_DATE,BV_LM,IN_CH,LOCA_CODE,LOCA_NAME,ZONE

然后你只用

上的左手表加入
INV_DATE

如果您不希望它引入倍数,则将其折叠到正确的级别或将其连接到足够的键上。