【问题标题】:SQL Query: Indicate if a row had a value of 0SQL 查询:指示某行的值是否为 0
【发布时间】:2021-03-02 11:25:19
【问题描述】:

如果我有一张记录每天售出多少产品的表格 - 如果售出 0 件商品则不输入日志 - 我将如何确定我的所有产品在一天的销售额为 0?

咖啡桌示例

Date    Item    QuantitySold
Jan 1   coffee  4
Jan 1   tea     1
Jan 2   tea     3
Jan 3   coffee  2
Jan 3   tea     4

所需的输出将是这样的。到目前为止,我可以得到一个列出每天和销售数量的版本,但不知道是否有办法同时“标记”产品。

Item    TotalSold   SoldDaily
coffee     6           FALSE
tea        8           TRUE

这是我粗略的起点 - JOIN 是试图强制显示所有日期,但它仍然在 1 月 2 日跳过咖啡行。从那里开始,我无法弄清楚我可能会如何执行下一步 - 我尝试了几个 CASE WHEN,但没有走到一起。

with dates as (select distinct date from cafetable)
 
 select date, 
 product,
 sum(quantitysold) as quant_sold,
 min(quantitysold) as min_sold
 from dates c
 left join cafetable d on d.date=c.date
 group by 1,2

我认为我什至没有很好地表达这一点。我每天要处理这个表十几次,我觉得这个查询已经把我的大脑烧坏了。

【问题讨论】:

  • 在您的查询中,您将product 作为第二列,但在CafeTable 中,我认为该列应该是item

标签: mysql sql google-bigquery


【解决方案1】:

您可以在这里使用逻辑来断言所有日期是否存在:

以下是 BigQuery 标准 SQL:

select
    c.date, 
    d.product,
    sum(quantitysold) as quant_sold,
    min(quantitysold) as min_sold,
    case when count(distinct c.date) = (select count(distinct date) from dates)
         then TRUE else FALSE end AS SoldDaily
from dates c
left join cafetable d
    on d.date = c.date
group by
    1, 2;

【讨论】:

  • @attractivetune 如果我在那里发帖,我将需要大约 20-30 分钟来研究它。是的,Wiktor 关闭了太多重复的问题,但并非他所有的重复标记都是错误的 IMO,只有其中一些。
【解决方案2】:

以下是 BigQuery 标准 SQL

#standardSQL
select item, 
  sum(QuantitySold) as TotalSold,
  count(*) = date_diff(max(date), min(date), day) + 1 as SoldDaily
from `project.dataset.CafeTable`
group by item   

您可以使用您问题中的示例数据进行测试,如以下示例所示

#standardSQL
with `project.dataset.CafeTable` as (
  select date '2020-01-01' date, 'coffee' item, 4 QuantitySold union all
  select '2020-01-01', 'tea', 1 union all
  select '2020-01-02', 'tea', 3 union all
  select '2020-01-03', 'coffee', 2 union all
  select '2020-01-03', 'tea', 4 
)
select item, 
  sum(QuantitySold) as TotalSold,
  count(*) = date_diff(max(date), min(date), day) + 1 as SoldDaily
from `project.dataset.CafeTable`
group by item   

有输出

【讨论】:

    【解决方案3】:

    如果有几天根本没有销售或在报告的最后一天销售为零,那么您可以使用GENERATE_DATE_ARRAY

    WITH test_table AS (
      SELECT DATE '2020-01-01' AS Date, 'coffee' AS Item, 4 AS QuantitySold UNION ALL
      SELECT DATE '2020-01-01', 'tea', 1 UNION ALL
      SELECT DATE '2020-01-02', 'tea', 3 UNION ALL
      SELECT DATE '2020-01-03', 'coffee', 2 UNION ALL
      SELECT DATE '2020-01-03', 'tea', 4
    )  
    , all_items_table AS (
      SELECT DISTINCT Item
      FROM test_table
    )
    SELECT *
    FROM all_items_table 
    CROSS JOIN UNNEST(GENERATE_DATE_ARRAY('2020-01-01', '2020-01-03')) AS Date
    LEFT JOIN test_table USING (Date, Item)
    

    WITH test_table AS (
      SELECT DATE '2020-01-01' AS Date, 'coffee' AS Item, 4 AS QuantitySold UNION ALL
      SELECT DATE '2020-01-01', 'tea', 1 UNION ALL
      SELECT DATE '2020-01-02', 'tea', 3 UNION ALL
      SELECT DATE '2020-01-03', 'coffee', 2 UNION ALL
      SELECT DATE '2020-01-03', 'tea', 4
    )  
    , all_items_table AS (
      SELECT DISTINCT Item
      FROM test_table
    )
    SELECT 
      Item,
      LOGICAL_AND(QuantitySold IS NOT NULL) AS SoldDaily
    FROM all_items_table 
    CROSS JOIN UNNEST(GENERATE_DATE_ARRAY('2020-01-01', '2020-01-03')) AS Date
    LEFT JOIN test_table USING (Date, Item)
    GROUP BY Item
    

    【讨论】:

      猜你喜欢
      • 2015-12-13
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      相关资源
      最近更新 更多