【问题标题】:I need to know the growth rate of sales in 2020 to 2021 using 2 tables on iowa_liquor_sales_forecasting dataset in google bigquery我需要使用 google bigquery 中 iowa_liquor_sales_forecasting 数据集上的 2 个表知道 2020 年到 2021 年的销售额增长率
【发布时间】:2022-10-14 17:14:17
【问题描述】:

我正在使用此查询,但没有要显示的数据。我的查询有什么问题?

with t1 as
(
    select
        extract (year from date) as year,
        sum (sale_dollars) as sales_year0
    from `bigquery-public-data.iowa_liquor_sales_forecasting.2020_sales_train`
    group by 1
),
t2 as
(
    select
        extract (year from date) as year,
        sum (sale_dollars) as sales_year1
    from `bigquery-public-data.iowa_liquor_sales_forecasting.2021_sales_predict`
    group by 1
)
select *,
    round((sales_year1 - lag(sales_year0) over(order by t1.year asc)) / lag(sales_year0) over(order by t1.year asc) * 100,2)||'%' as growth_rate
from t1
join t2 on t1.year=t2.year

我的预期回报

year | total_sales | growth_rate |
2020 | ........... | ........... |
2021 | ........... | ........... |

【问题讨论】:

    标签: sql google-bigquery


    【解决方案1】:

    BigQuery 中的JOIN 本身(没有left,outer,...)是inner join,这意味着左边的表和右边的表必须共享一个条件才能产生结果。

    目前,t1 仅具有 2020 年,t2 仅具有 2021 年,这导致连接条件 join t2 on t1.year=t2.year 为假。

    FROM A [INNER] 加入 B 使用 (x)

    我猜你想要做的是计算growth_rate,下面是获取价值的一种方法。

    with
    t1 as (
        select
            extract (year from date) as year_2020,
            sum (sale_dollars) as sales_year_2020
        from `bigquery-public-data.iowa_liquor_sales_forecasting.2020_sales_train`
        group by 1
    ),
    t2 as (
        select
            extract (year from date) as year_2021,
            sum (sale_dollars) as sales_year_2021
        from `bigquery-public-data.iowa_liquor_sales_forecasting.2021_sales_predict`
        group by 1
    )
    select
        *,
        -- round((year_2021 - year_2020) / year_2020 * 100, 3) as growth_rate  -- typo (wrong column names)
        round((sales_year_2021 - sales_year_2020) / sales_year_2020 * 100, 3) as growth_rate
    from t1 join t2 on t1.year_2020 = (t2.year_2021 - 1)
    ;
    

    【讨论】:

    • 谢谢回答我的问题。但是growth_rate的结果是不可能的,因为2020年的总销售额是396,663,131,而2021年的总销售额是94,938,294意味着增长率应该是负数但结果是正数 0.0495%
    • 哦,我的错,我打错了......我修正了答案(应该是-76.066%)。 .
    • 我懂了。再次感谢您的回答。我想问一些事情。 (t2.year_2021 -1) -1 背后的含义是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    相关资源
    最近更新 更多