db2 每年独特口味的冰淇淋数量

oaxa6hgo  于 12个月前  发布在  DB2
关注(0)|答案(2)|浏览(156)

下面是一张不同年份不同口味冰淇淋的table(名称: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

CREATE TABLE ice_cream_table  (
  "Year" INT,
  "Flavor" VARCHAR(100)
);

INSERT INTO ice_cream_table 
  ("Year", "Flavor")
VALUES
  ('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;


我的输出:

+------+-----------+----------------+--------------+
| Year | new_flavor | repeated_flavor| total_flavor |
+------+-----------+----------------+--------------+
| 2008 | 3         | NULL           | 3            |
| 2013 | 1         | 2              | 3            |
| 2017 | 2         | 2              | 4            |
| 2019 | 2         | 1              | 3            |
| 2022 | 1         | 3              | 4            |
+------+-----------+----------------+--------------+


我做得对吗?我对Python更有经验,对SQL更少......代码运行,但我不确定我的逻辑是否抓住了本质。

guykilcj

guykilcj1#

您可以使用ROW_NUMBERLAG查找每个flavor的第一行,然后按year分组并向上计数。
与其他答案相比,这有一个好处,因为它只需要对基表进行一次扫描。

SELECT
  Year,
  COUNT(IsFirst) AS NewFlavors,
  COUNT(DISTINCT Flavor) - COUNT(IsFirst) AS RepeatedFlavors,
  COUNT(DISTINCT Flavor) AS TotalFlavors
FROM (
    SELECT *,
      CASE WHEN ROW_NUMBER() OVER (PARTITION BY Flavor ORDER BY Year) = 1 THEN 1 END AS IsFirst
    FROM ice_cream_table icf
) icf
GROUP BY
  Year;

字符串
db<>fiddle

sqserrrh

sqserrrh2#

您可以使用GROUP BYLEFT 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

字符串

相关问题