这里有一张不同年份不同口味冰淇淋的桌子(名称:ice_cream_table):
Year Flavor
2008 Mint
2008 Mint
2008 Cookie Dough
2008 Cookie Dough
2008 Pistachio
2013 Chocolate
2013 Cookie Dough
2013 Pistachio
2013 Chocolate
2013 Pistachio
2017 Chocolate
2017 Vanilla
2017 Chocolate
2017 Cookie Dough
2017 Strawberry
2019 Mango
2019 Lemon
2019 Vanilla
2019 Mango
2019 Mango
2022 Chocolate
2022 Chocolate
2022 Mint
2022 Strawberry
2022 Cherry
每年我都想知道:有多少种口味是第一次出现,有多少种口味是往年出现的?
这是我的代码:
with yearly_flavor as (
select
year,
flavor,
row_number() over (partition by flavor order by year) as rn
from
ice_cream_table
group by
flavor, year
),
new_flavor as (
select
year,
count(flavor) as new_flavor
from
yearly_flavor
where
rn = 1
group by
year
),
repeated_flavor as (
select
year,
case
when count(flavor) is null then 0
else count(flavor)
end as repeated_flavor,
count(flavor) as new_flavor
from
yearly_flavor
where
rn > 1
group by
year
),
total_flavor as (
select
year,
count(distinct flavor) as total_flavor
from
ice_cream_table
group by
year
)
select
n.year,
n.new_flavor,
r.repeated_flavor,
t.total_flavor
from
new_flavor n
left join
repeated_flavor r on n.year = r.year
join
total_flavor t on n.year = t.year
order by
n.year;
我做得正确吗?我对 Python 更有经验,对 SQL 经验较少……代码可以运行,但我不确定我的逻辑是否抓住了本质。
您可以按如下方式使用
GROUP BY
和 LEFT JOIN
:
select t.year,
count(distinct t.flavor) - count(distinct tt.flavor) as new_flavor
count(distinct tt.flavor) as repeated_flavor,
count(distinct t.flavor) as total_flavor
from ice_cream_table t
left join ice_cream_table tt on t.year > tt.year and t.flavor = tt.flavor
group by t.year