如何基于参考表上的最新日期在联接中生成每日得分

问题描述 投票:0回答:1

SQL Vertica问题(但可以接受任何SQL查询答案)我有一个当前处于“ day-url”级别的表“ Base”,并且我尝试通过加入“ day-url”级别的参考表“ Score Update”来获取每天每个URL的“得分”。问题是参考表不是每天,而是特定日期级别。我需要将“基准”中“日期”之前的“分数更新”中的分数与“更新日期”一起加入。下面的示例:(请参阅底部的图片)

我拥有:(基本)日期网址得分8/1 A8/1乙8/2安8/3安8/9乙8/10安8/11 AB / 11 B8/13 B

(评分更新)

更新日期URL分数

7/25 A x

7/25 B y

8/10 A alpha

8/10 B beta

我想要的是:我所拥有的:(基础)日期网址得分8/1 A x

8/1 B y

8/2 A x

8/3 A x

8/9 B y

8/10 A alpha

8/11 A alpha

B / 11 B beta

8/13 B Beta

enter image description here

并且两个表都将继续更新以获取将来的日期数据。谁能帮我如何加入这两个表?

谢谢!

sql date join vertica
1个回答
0
投票

你的意思是这个吗?

WITH
-- your input ...
base(dt,url,score) AS (
          SELECT DATE '2019-08-01','A',NULL
UNION ALL SELECT DATE '2019-08-01','B',NULL
UNION ALL SELECT DATE '2019-08-02','A',NULL
UNION ALL SELECT DATE '2019-08-03','A',NULL
UNION ALL SELECT DATE '2019-08-04','B',NULL
UNION ALL SELECT DATE '2019-08-05','A',NULL
UNION ALL SELECT DATE '2019-08-06','A',NULL
UNION ALL SELECT DATE '2019-08-10','A',NULL
UNION ALL SELECT DATE '2019-08-11','A',NULL
UNION ALL SELECT DATE '2019-08-11','B',NULL
UNION ALL SELECT DATE '2019-08-12','A',NULL
UNION ALL SELECT DATE '2019-08-13','A',NULL
)
,
-- your input ...
score_upd(upd_dt,url,score) AS (
          SELECT DATE '2019-07-25','A','x'
UNION ALL SELECT DATE '2019-07-25','B','alpha'
UNION ALL SELECT DATE '2019-08-10','A','y'
UNION ALL SELECT DATE '2019-08-10','B','beta'
)
-- real select starts here ..                                     
SELECT
  b.dt
, b.url
, u.score
FROM base b
LEFT JOIN score_upd u
  ON b.url=u.url
 AND b.dt INTERPOLATE PREVIOUS VALUE u.upd_dt
ORDER BY dt,url
;
-- out      dt     | url | score 
-- out ------------+-----+-------
-- out  2019-08-01 | A   | x
-- out  2019-08-01 | B   | alpha
-- out  2019-08-02 | A   | x
-- out  2019-08-03 | A   | x
-- out  2019-08-04 | B   | alpha
-- out  2019-08-05 | A   | x
-- out  2019-08-06 | A   | x
-- out  2019-08-10 | A   | y
-- out  2019-08-11 | A   | y
-- out  2019-08-11 | B   | beta
-- out  2019-08-12 | A   | y
-- out  2019-08-13 | A   | y
-- out (12 rows)
© www.soinside.com 2019 - 2024. All rights reserved.