鉴于这张原始表格
Record
:
food | food_preference | count
--------------------------------
burger | [burger] | 100
burger | [burger, pizza] | 70
pizza | [burger, pizza] | 130
burger | [burger, corn] | 25
corn | [burger, corn] | 25
我如何迭代所有独特的
food
将以下值相加
食物
在里面
food_preference
给予:
food | count
--------------
burger | 350
pizza | 200
corn | 50
我尝试进行病例匹配,但不确定如何迭代食物价值。
WITH Record AS
(
SELECT * FROM (
values
('burger', '[burger]', 100),
('burger', '[burger, pizza]', 70),
('pizza', '[burger, pizza]', 130),
('burger', '[burger, corn]', 25),
('corn', '[burger, corn]', 25)
) x(food, food_preference, count)
)
SELECT
food,
CASE
-- How to sum the count value if the food is in the food_preference?
END AS total_count
FROM Record