【问题标题】:Selecting another column from a query with a different date filter从具有不同日期过滤器的查询中选择另一列
【发布时间】:2023-01-27 22:54:18
【问题描述】:

我正在处理一些销售数据并提取我在过滤器中定义的特定一周的指标。 但是,我想在我的查询中添加另一列 (first_sale_date)。这将显示此 asin/mp 组合第一次出现在我的表中,而不管我尝试为其提取其他指标的日期过滤器如何。

因为我已经 按日期过滤 我不知道如何回顾表中的所有数据以找到它在我过滤的那一周之前的第一次出现。


select date,
    ,asin
    ,marketplace
    ,SUM(ordered_product_sales) as OPS
    ,SUM(cogs) as cogs
    **,min(date) as first_sale_date**

from prod.sales

where date > '2023-01-01'

group by 1,2,3,4

【问题讨论】:

标签: sql


【解决方案1】:

您可以为此使用相关子查询

select year
    ,week
    ,asin
    ,marketplace
    ,SUM(ordered_product_sales) as OPS
    ,SUM(cogs) as cogs
    ,( SELECT MIN(date) FROM sales sal1 WHERe sal1.asin = sal.asin and sal1.marketplace = sal.marketplace) as first_sale_date

from prod.sales sal

where year = '2023' and week = '1'

group by 1,2,3,4

【讨论】:

    猜你喜欢
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多