sql:基于另一个表设置条件值,带有日期间条件

mwyxok5s  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(251)

对sql有点陌生,在网上做了一些搜索,但找不到答案。
sql数据库是一个雪花数据库,我相信它是一个ansi数据库。
我有一个事实表概述如下。由于每次报告新问题时都会添加新记录,因此可以将相同的问题/upc/仓库/日期组合在一起。

exclude列就是我想弄清楚的—如果issue/upc/warehouse和date的组合在exclude表中,它应该是y,如下所示。

棘手的是,数据是在一天的水平,但我想排除它,如果它在范围之内。
因此,只有i-100/1234/china/5-5-2019和i-324/1349/newyork/6-1-2019应排除在外,因为它们与排除表中的值和日期范围匹配。
我尝试了下面的查询,但select for dates between返回了多条记录,并出现此错误 Single-row subquery returns more than one row. ```
update "FactDetails"
set "EXCLUDE" = 'Y'

where ("UPC","Warehouse", "Issue") in
("UPC","Warehouse", "Issue" from "Exclusions"
where "PLANT_NUMBER" is not null
and "ISSUE_CODE" is not null)

and "Date" between
(select "DATE_FROM" from "Exclusion"
where "PLANT_NUMBER" is not null
and "ISSUE_CODE" is not null) and
(select "DATE_TO" from "Exclusion"
where "PLANT_NUMBER" is not null
and "ISSUE_CODE" is not null);

另一个复杂性是,排除表可以通过upc/issue/warehouse的组合排除“levels”。仓库是最具体的,如果只是upc,它涵盖了最多的数据。
![](https://i.stack.imgur.com/EcZOr.png)
gmxoilav

gmxoilav1#

如果我理解正确,你可以用 exists :

update t
    set exclude = 'Y'
    where exists (select 1
                  from exclusions e
                  where e.issue_code = t.issue_code and
                        e.upc = t.upc and
                        e.warehouse = t.warehouse and
                        t.date between e.date_from and e.date_to
                 );

相关问题