“如何替换”;计划的;加上;“实际值”;如果数据使用SQL存在
我有一张像这样的桌子 它看起来像什么: 日期 电影 位置 类型 意见 11/19/2020 超人 纽约 计划 11/26/2020 超人 纽约 计划 35 12/3/2020 超人 纽约 计划 20 12/10/2020 超人 纽约 计划 20 12/10/2020 超人 纽约 现货 45 12/17/2020 超人 纽约 计划 30 12/17/2020 超人 纽约 现货 53 12/24/2020 超人 纽约 计划 40 12/24/2020 超人 纽约 现货 55 12/31/2020 超人 纽约 计划 40 12/31/2020 超人 纽约 现货 64 1/7/2021 超人 纽约 计划 70 1/7/2021 超人 纽约 现货 75 1/14/2021 超人 纽约 计划 30 1/14/2021 超人 纽约 现货 54 1/21/2021 超人 纽约 计划 10 1/21/2021 超人 纽约 现货 15 1/28/2021 超人 纽约 计划 10 1/28/2021 超人 纽约 现货 2/4/2021 超人 纽约 计划 14“如何替换”;计划的;加上;“实际值”;如果数据使用SQL存在,sql,filtering,snowflake-cloud-data-platform,Sql,Filtering,Snowflake Cloud Data Platform,我有一张像这样的桌子 它看起来像什么: 日期 电影 位置 类型 意见 11/19/2020 超人 纽约 计划 11/26/2020 超人 纽约 计划 35 12/3/2020 超人 纽约 计划 20 12/10/2020 超人 纽约 计划 20 12/10/2020 超人 纽约 现货 45 12/17/2020 超人 纽约 计划 30 12/17/2020 超人 纽约 现货 53 12/24/2020 超人 纽约 计划 40 12/24/2020 超人 纽约 现货 55 12/31/2020 超
一种方法是
不存在
:
select t.*
from t
where t.type = 'Actuals' or
not exists (select 1
from t t2
where t2.date = t.date and
t2.movie = t.movie and
t2.location = t.location and
t2.type = 'Actuals'
);
一种有趣的方法是使用聚合:
select date, movie, location,
coalesce( max(case when type = 'Actuals' then type end),
max(type)
) as type,
coalesce( max(case when type = 'Actuals' then views end),
max(views)
) as views
from t
group by date, movie, location;
但最常见的方法是使用带有窗口功能的qualify
:
select t.*
from t
qualify row_number() over (partition by date, movie, location
order by type
) = 1;
注:订单按字母顺序排列,“实际值”<“计划值”