左联接后右侧 table 列中的重复值
Duplicate Values in the right table columns after left join
同时使用左连接连接 invmast 和 cadv table 添加两个新列 coupon_pos 作为 coupon_pos_c 和 coupon_web 作为 coupon_web_c ,我从右边的列中得到重复值 table (cadv).
/*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;
您的右手 table 查询按
分组
INV_DATE,BV_LM,IN_CH,LOCA_CODE,LOCA_NAME,ZONE
然后你就在
上用左手 table 加入了
INV_DATE
如果您不希望它引入倍数,则将其折叠到正确的级别或加入足够多的键。
每次在 inv_date 上进行匹配时,您都会从右侧 table 复制聚合值。尝试向连接语句添加条件,例如
/*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;
同时使用左连接连接 invmast 和 cadv table 添加两个新列 coupon_pos 作为 coupon_pos_c 和 coupon_web 作为 coupon_web_c ,我从右边的列中得到重复值 table (cadv).
/*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;
您的右手 table 查询按
分组INV_DATE,BV_LM,IN_CH,LOCA_CODE,LOCA_NAME,ZONE
然后你就在
上用左手 table 加入了INV_DATE
如果您不希望它引入倍数,则将其折叠到正确的级别或加入足够多的键。
每次在 inv_date 上进行匹配时,您都会从右侧 table 复制聚合值。尝试向连接语句添加条件,例如
/*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;