0

This is the first time I've created a SQL with CASE. I must have done something wrong with WHEN and ELSE.

I would like to obtain the condition that:

  • If team_away....is contained in the round column which has 1...and if team_away is contained in the team_away column....then it is OK

  • If team_away....is contained in the round column which has 1...and if team_away is contained in the team_home column....then it is NULL

Notice that team_away=? is both a variable and the column name

What am I doing wrong?

SELECT team_home, team_away, score_home, score_away 
FROM Results
WHERE

    (CASE
       WHEN current_round= 1 AND team_home = team_away=? THEN NULL
       ELSE current_round= 1 AND team_away = team_away=?
    END)

LIMIT 1;            
''', [Match_Team_Away, Match_Team_Away])

Maybe the problem is in the syntax setting: AND team_away = team_away=? or team_home = team_away=? (two =)

UPDATE:

This is table Result:

current_round team_home team_away score_home score_away
1 Liverpool United 3 0
1 Arsenal Aston Villa 4 1

For example if my team_away variable (team_away=?) is United, it means it is in the team_away table as you can see. In this case I would like to use the second CASE (the one with ELSE). Whereas if United had been in the team_home table and not in team_away, then I would have liked to receive a NULL

16
  • Case statement in sql is as follows: CASE WHEN expression1 AND/OR expression2 AND/OR expression3.... THEN value1 WHEN expression4 AND/OR expression5 AND/OR expression6.... THEN value2 ... ELSE value END as column_name also find an alternate for double equal in single expression as it doesn't make sense to SQL
    – Sam
    Sep 22 at 6:05
  • @Sam I don't understand much from the formatting of your code in the comment. Could you show me a solution using my values please? Sep 22 at 6:08
  • @EvangelosDellas the question is unclear for multiple reasons. There are no variables in queries, but there may be parameters. team_away=? isn't a variable or parameter name, it's an equality check between team_away and a nameless parameter ?. CASE is an expression that returns a value, not a way to group conditions. You can compare its return value to something else if you want, but that usually leads to bad performance. If you want to combine options, use OR. The evaluation order doesn't matter Sep 22 at 7:55
  • You probably do not need to use case when in a where statement. case when is used to create a new column based on some conditions. where is used to filter. What do you want to do ? filter the lines or create something new ?
    – Steven
    Sep 22 at 7:55
  • The conditions you describe are unclear too. If team_away....is contained in the round column which has 1...and if team_away is contained in the team_away column....then it is OK what does that mean? One column is contained in another? But both are equal to 1? Or a column is contained in itself? Sep 22 at 7:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.