SQL 根据注释单元格的多条件测试生成分数列
SQL generate score column from multi-condition test on a notes cell
基本上我有一个 table 和一个注释列,我想生成一个查找一些条件的列,并根据查看每个注释时满足的条件数得出一个分数。我可能会以错误的方式解决这个问题,所以请随意切线。
目前查询:
SELECT
SUM(
SUM(case when notes like '%Tuna%' THEN 1 ELSE 0 END)
SUM(case when notes like '%apple%' THEN 1 ELSE 0 END)
SUM(case when notes like '%burrito' THEN 1 ELSE 0 END)
-- ) as score
,Name
,Date
,Notes
FROM food_jrnl
示例 table food_jrnl:
Name Date Note
Peter 6/1/2016 Just mountain Dew and cheatos
Jimmy 5/25/2016 Chocolate cake, cheesy potatoes and ketchup
Sophie 5/16/2016 just grits and tuna!!
Bianca 5/9/2016 Chocolate milk, Ahi tuna, Gala apple
Sam 4/23/2016 Tuna salad
Josh 1/10/2016 Had a banana and apple with orange juice
我希望创造的:
Score Name Date Note
0 Peter 6/1/2016 Just mountain Dew and cheatos
0 Jimmy 5/25/2016 Chocolate cake, cheesy potatoes and ketchup
1 Sophie 5/16/2016 just grits and tuna!!
2 Bianca 5/9/2016 Chocolate milk, Ahi tuna, Gala apple
1 Sam 4/23/2016 Tuna salad
0 Josh 1/10/2016 Had a banana and apple with orange juice
实际上你不需要为此使用 sum
:
SELECT
case when notes like '%Tuna%' THEN 1 ELSE 0 END +
case when notes like '%apple%' THEN 1 ELSE 0 END +
case when notes like '%burrito' THEN 1 ELSE 0 END as score
,Name
,Date
,Notes
FROM food_jrnl
基本上我有一个 table 和一个注释列,我想生成一个查找一些条件的列,并根据查看每个注释时满足的条件数得出一个分数。我可能会以错误的方式解决这个问题,所以请随意切线。
目前查询:
SELECT
SUM(
SUM(case when notes like '%Tuna%' THEN 1 ELSE 0 END)
SUM(case when notes like '%apple%' THEN 1 ELSE 0 END)
SUM(case when notes like '%burrito' THEN 1 ELSE 0 END)
-- ) as score
,Name
,Date
,Notes
FROM food_jrnl
示例 table food_jrnl:
Name Date Note
Peter 6/1/2016 Just mountain Dew and cheatos
Jimmy 5/25/2016 Chocolate cake, cheesy potatoes and ketchup
Sophie 5/16/2016 just grits and tuna!!
Bianca 5/9/2016 Chocolate milk, Ahi tuna, Gala apple
Sam 4/23/2016 Tuna salad
Josh 1/10/2016 Had a banana and apple with orange juice
我希望创造的:
Score Name Date Note
0 Peter 6/1/2016 Just mountain Dew and cheatos
0 Jimmy 5/25/2016 Chocolate cake, cheesy potatoes and ketchup
1 Sophie 5/16/2016 just grits and tuna!!
2 Bianca 5/9/2016 Chocolate milk, Ahi tuna, Gala apple
1 Sam 4/23/2016 Tuna salad
0 Josh 1/10/2016 Had a banana and apple with orange juice
实际上你不需要为此使用 sum
:
SELECT
case when notes like '%Tuna%' THEN 1 ELSE 0 END +
case when notes like '%apple%' THEN 1 ELSE 0 END +
case when notes like '%burrito' THEN 1 ELSE 0 END as score
,Name
,Date
,Notes
FROM food_jrnl