我希望在SQL查询方面得到一些帮助,以便将最新数据提取到昨天。
我有一个名为clickstream的主表,这个表包含所有的网站活动,还有一个名为businesseffectivedate的日期列,它提供了每天的表现(访问、点击、销售)的详细信息。
我根据我的需求创建了一个查询,我称之为TableA。我的目标是每天根据业务更新TableA。
我想要的数据是,从8月1日开始,对于结束日期,我想获得最新的数据(即昨天的9月23日),我尝试使用“current_date()”inside Where子句来获取每天的最新数据。
我本来希望,我能根据current_date()得到昨天之前的最新数据,但我仍然看到最新的日期是9月22日(即使我今天刷新了数据,理想的日期应该是9月23日)。
第一次查询(已尝试但不起作用):
DROP TABLE IF EXISTS db.tableA;
CREATE TABLE db.tableA as
Select clicks, impressions, businesseffectivedate, sales from clickstream
where businesseffectivedate > '2022-07-31' and businesseffectivedate < current_date() 第二次查询(已试但不起作用):
DROP TABLE IF EXISTS db.tableA;
CREATE TABLE db.tableA as
Select clicks, impressions, businesseffectivedate, sales from clickstream
where businesseffectivedate > '2022-07-31' and businesseffectivedate <= date_add(current_date(),-1)第三个查询(尚未尝试):
DROP TABLE IF EXISTS db.tableA;
CREATE TABLE db.tableA as
Select clicks, impressions, businesseffectivedate, sales from clickstream
where businesseffectivedate > '2022-07-31' and businesseffectivedate < (select max(businesseffectivedate) from clickstream)对于我运行此命令的clickstream数据,从clickstream /中选择max(businesseffectivedate),这将给出2022-09-23的答案(正确)
我对我的tableA运行了相同的命令,因为这个表中的数据来自clickstream。
select max(businesseffectivedate) from tableA // I am seeing 2022-09-22 (in-correct) 请告诉我如何使这个查询自动化(即每天获取最新数据)。FYI,我是SQL新手,不熟悉代码,不熟悉行/分区等等。
谢谢!
发布于 2022-09-24 18:49:58
我认为这将提取一天的数据(MySQL)
DROP TABLE IF EXISTS db.tableA;
CREATE TABLE db.tableA as
Select clicks, impressions, businesseffectivedate, sales from clickstream
where DATE(businesseffectivedate) = '2022-07-31' https://stackoverflow.com/questions/73837723
复制相似问题