sql:日历表上的左联接(spark sql)

z8dt9xmd  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(271)

我正在尝试将数据连接到一个与用户id交叉连接的日历表,以获得与之对应的其他列。我试过按日期条件加入,但没有日期条件。创建了一个交叉连接的主表,以左连接上的其他数据。然而,似乎我错过了什么。
日期如下所示:

CAL_DT  BUYER_ID
2019-03-31  1
2019-03-31  2
2019-03-31  3
2019-03-30  1
2019-03-30  2
2019-03-30  3
2019-03-29  1
2019-03-29  2
2019-03-29  3 ......

数据2看起来像:

CREATED_DT  BUYER_ID ITEM_PRICE
2019-03-31  1   10
2019-03-30  2.  12
2019-03-29  3.  45
2019-03-29  2.  13 ........

这是我的密码:

WITH DATE_TBL AS
(
SELECT CAL.CAL_DT, CK.BUYER_ID
FROM DATA1 CAL
CROSS JOIN DATA2 CK
WHERE cal.CAL_DT BETWEEN '2018-01-01' AND '2019-03-31'
AND CK.BYR_CNTRY_ID IN (1,2,3) AND CK.CREATED_DT BETWEEN '2019-03-01' AND '2019-03-31'
GROUP BY 1,2
)
,
REVENUE_CALC AS 
(
SELECT CAL.CAL_DT
      ,CK.BYR_CNTRY_ID
      ,CK.BUYER_ID
      ,CK.CREATED_DT AS CREATED_DT
      ,SUM(CK.ITEM_PRICE) AS ITEM_PRICE
      ,SUM(CK.QUANTITY) AS QUANTITY
      ,MAX(COALESCE(I.CURNCY_PLAN_RATE, 1)) AS CURNCY_PLAN_RATE

      ,SUM(CK.ITEM_PRICE *CK.QUANTITY *I.CURNCY_PLAN_RATE) AS REVENUE

      FROM DATE_TBL CAL
      LEFT JOIN DATA2 CK
      ON CAL.BUYER_ID = CK.BUYER_ID AND CAL.CAL_DT = CK.CREATED_DT

      LEFT JOIN DATA3 I
      ON I.CURNCY_ID   = CK.LSTG_CURNCY_ID

      GROUP BY 1,2,3,4
      ORDER BY CAL.CAL_DT DESC, CK.BUYER_ID
)
SELECT *
FROM REVENUE_CALC

预期结果必须如下所示:

CAL_DT  BUYER_ID ITEM ITEM_PRICE
2019-03-31  1.    10
2019-03-31  2.    null
2019-03-31  3.    null
2019-03-30  1.    null
2019-03-30  2.    12
2019-03-30  3.    null
2019-03-29  1.    null
2019-03-29  2.    13
2019-03-29  3.    45......

我得到的只是普通日期的数据。有人能帮我理解我做错了什么吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题