2016-06-10 6 views
1

Grundsätzlich habe ich eine Tabelle mit einer Notiz Spalte und ich möchte eine Spalte, die nach ein paar Bedingungen sucht und spuckt eine Punktzahl basierend auf wie viele Bedingungen erfüllt wurden wenn man jede Note betrachtet. Ich gehe vielleicht in die falsche Richtung, also bitte tangieren.SQL Spalte aus Multi-Bedingungs-Test auf einer Notizzelle generieren

Abfrage so weit:

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 

Beispieltabelle 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 

Was ich erstellen bin der Hoffnung:

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 

Antwort

1

Eigentlich brauchen Sie nicht sum zu verwenden dies:

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 
+0

Ich habe nicht realisiert, dass mehrere Summen addiert werden könnten, aber das macht Sinn, wenn ich es sehe. Wusste auch nicht, dass es eine Gruppe für alle Spalten benötigt! Du bist der beste! - Ich muss gepostet haben, nachdem du deine Antwort geändert hast. Der Fall wann ist viel schlanker, braucht die Gruppe nicht durch! Vielen Dank! – jbird468

Verwandte Themen