Dom
Dom

Reputation: 1

CASE after JOIN LEFT in SQL

I'm having a bit of a struggle with a SQL query. I have two tables and successfully created a query to display the needed information with LEFT JOIN. Now I want to edit the result in a way, where I am adding / transforming information depending on whether a row contains anything or is set to NULL.

Here is my query:

SELECT users.user_uid, daten.tab_lkw, daten.feld0
FROM users
LEFT JOIN (SELECT DISTINCT daten.feld0, daten.tab_lkw FROM daten WHERE daten.feld0 = '2018-06-22' ) daten ON users.user_uid = daten.tab_lkw
WHERE users.user_uid NOT LIKE 'chef'
ORDER BY users.user_uid

The result I get is the following:

result

Now comes the tricky part (at least for me). I want to add a column that displays either 'no' when feld0 is containing NULL or a 'yes' otherwise.

No matter what I do, I seem to get a SQL syntax error. To be honest, I don't really know where to put the CASE logically.

Somebody got an idea?

Big thanks

Upvotes: 0

Views: 62

Answers (1)

Shushil Bohara
Shushil Bohara

Reputation: 5656

Its exactly what I understood from your question, hope it will work for you. I have added a column YesNo based on the value of daten.feld0

SELECT users.user_uid, daten.tab_lkw, daten.feld0,
    CASE WHEN daten.feld0 IS NULL THEN 'No' ELSE 'Yes' END AS YesNo
FROM users
LEFT JOIN (SELECT DISTINCT daten.feld0, daten.tab_lkw 
            FROM daten 
            WHERE daten.feld0 = '2018-06-22' ) daten ON users.user_uid = daten.tab_lkw
WHERE users.user_uid NOT LIKE 'chef'
ORDER BY users.user_uid

Upvotes: 1

Related Questions