我目前正在研究sql,但存在以下问题:
请考虑下表:
______________________________
Food | Ingredients
______________________________
Soup | Pepper
Soup | Salt
Soup | Salt
Soup | Meat
Main | Salt
Main | Salt
Main | Meat
我想做的是计算每种独特食物价值的成分数,以便产生这张表。
________________________________________
Food | Ingredients | Count
________________________________________
Soup | Pepper | 1
Soup | Salt | 2
Soup | Salt | 2
Soup | Meat | 1
Main | Salt | 3
Main | Salt | 3
Main | Salt | 3
Main | Meat | 2
Main | Meat | 2
人们将如何为此创建查询?谢谢!
答案 0 :(得分:2)
这将在SQL Server中工作(如果您正在使用的话)
SELECT Food, Ingredients, COUNT(Ingredients) AS IngredientCount
FROM TABLE
GROUP BY Food, Ingredients
答案 1 :(得分:1)
如果我理解正确,则可以使用窗口功能:
select t.*, count(*) over (partition by Food, Ingredients) as cnt
from t;
当然,您想要的结果与输入数据不匹配,因此这是基于您对所需内容的解释。