根据日期范围排除某些产品

时间:2019-04-26 12:50:57

标签: sql google-bigquery

例如,我有1年的销售数据,并且某些产品在特定日期范围内不可用。

我目前有1个日期范围,但是如果有多个排除项,最佳做法是什么?

SELECT * FROM XXX
WHERE 
IF(Date BETWEEN '2018-11-22' AND '2019-03-28',
ID IN (8467,8468,8469,8470),
ID IN (8467,8468,8469,8470,9551,9552,9553)
)

特别是如果日期重叠,如何解决此问题?

3 个答案:

答案 0 :(得分:2)

如果您尝试排除值,我在想:

SELECT *
FROM XXX
WHERE ID IN (8467, 8468, 8469, 8470, 9551, 9552, 9553) AND
      (Date BETWEEN '2018-11-22' AND '2019-03-28' AND
ID NOT IN (9551, 9552, 9553) OR
       Date NOT BETWEEN '2018-11-22' AND '2019-03-28'
      );

您可以为其他日期添加多对。

要获得完整的解决方案,您可能需要创建一个包含以下列的表:

  • product_id
  • start_exclusion_date
  • end_exclusion_date

然后将查询的短语设置为:

select xxx.*
from xxx left join
     exclusions e
     on xxx.id = e.product_id and
        xxx.date >= e.start_exclusion_date and
        xxx.date <= e.end_exclusion_date
where xxx.id in ( . . . );

从长期来看,这可能更容易维护。

答案 1 :(得分:0)

尝试一下

create-react-app

答案 2 :(得分:0)

下面是BigQuery Standard SQL的示例,并显示了使用白名单和黑名单规则(全部具有相当简化的虚拟数据只是为了演示实际情况)构建“完整图片”的方向

#standardSQL
WITH `project.dataset.xxx` AS (
  SELECT 1 id, DATE '2018-11-22' `date` UNION ALL
  SELECT 2, '2018-11-23' UNION ALL
  SELECT 3, '2018-11-24' UNION ALL
  SELECT 4, '2018-11-25' UNION ALL
  SELECT 1, '2018-11-26' UNION ALL
  SELECT 2, '2018-11-27' UNION ALL
  SELECT 3, '2018-11-28' UNION ALL
  SELECT 8, '2018-11-29'
), `project.dataset.whitelist` AS (
  SELECT DATE '2018-11-22' start, DATE '2018-11-29' finish, [2,3] ids UNION ALL
  SELECT '2018-11-22', '2018-11-22', [1]
), `project.dataset.blacklist` AS (
  SELECT DATE '2018-11-26' start, DATE '2018-11-28' finish, [1,3] ids UNION ALL
  SELECT '2018-11-22', '2018-11-22', [10]
)
SELECT DISTINCT t.*
FROM `project.dataset.xxx` t
JOIN `project.dataset.whitelist` w
  ON (`date` BETWEEN w.start AND w.finish AND id IN UNNEST(w.ids))
JOIN `project.dataset.blacklist` b
  ON NOT(`date` BETWEEN b.start AND b.finish AND id IN UNNEST(b.ids))

有结果

Row id  date     
1   1   2018-11-22   
2   2   2018-11-27   
3   2   2018-11-23   
4   3   2018-11-28   
5   3   2018-11-24     

很明显,在所有情况下,所有涉及的表都是真实表,查询看起来就像下面的

#standardSQL
SELECT DISTINCT t.*
FROM `project.dataset.xxx` t
JOIN `project.dataset.whitelist` w
  ON (`date` BETWEEN w.start AND w.finish AND id IN UNNEST(w.ids))
JOIN `project.dataset.blacklist` b
  ON NOT(`date` BETWEEN b.start AND b.finish AND id IN UNNEST(b.ids))