0

I am using redshift, but I suspect this will be valuable to many other forms of SQL. I am also new to SQL and would appreciate any advice on my query, other than advice which pertains specifically to my question; just please keep it constructive.

QUESTION: How do I avoid returning rows where all rows of my result are NULL?

Here is my query:

SELECT 
    CASE WHEN events.event_id IN 
        (SELECT DISTINCT event_id FROM strings_ref WHERE string_id = 123)
        THEN event_id END AS 123, 
    CASE WHEN events.event_id IN 
        (SELECT DISTINCT event_id FROM strings_ref WHERE string_id = 456)
        THEN event_id END AS 456
    FROM events

There are many many other string_ids, and this query just returned 1000 consecutive NULLS. EDIT: I have verified that there are results.

NOTE: I have accepted the answer that most pertains to my question, but @gordon-linoff 's answer is more general.

1
  • 1
    I think this would be much clearer if you shared some sample data and your desired results. Your query is very strange and could be solved simply by aggregating and using a CASE statement instead of subquerying to return the same thing you are returning anyway. Commented Jul 9, 2018 at 18:21

2 Answers 2

1

Here's one way:

SELECT E.*, SR1.EVENT_ID, SR2.EVENT_ID
FROM Events E
    LEFT OUTER JOIN strings_ref sr1 on E.EVENT_ID=SR1.EVENT_ID AND sr1.string_id = 123
    LEFT OUTER JOIN strings_ref sr2 on E.EVENT_ID=SR2.EVENT_ID AND sr2.string_id = 456
WHERE SR1.EVENT_ID IS NOT NULL OR SR2.EVENT_ID IS NOT NULL

Join on strings_ref multiple times

Sign up to request clarification or add additional context in comments.

Comments

1

One method uses a subquery:

SELECT e.*
FROM (SELECT (CASE WHEN e.event_id IN (SELECT sr.event_id FROM strings_ref sr WHERE sr.string_id = 123)
                   THEN e.event_id
              END) AS e_123, 
             (CASE WHEN e.event_id IN (SELECT sr.event_id FROM strings_ref sr WHERE sr.string_id = 456)
                  THEN e.event_id
              END) AS e_456
      FROM events e
     ) e
WHERE e_123 IS NOT NULL or e_456 IS NOT NULL;

Some advice:

  • Qualify all column names in the query.
  • Use table aliases that are abbreviations of the table name.
  • SELECT DISTINCT is not needed with IN.
  • Give your columns names that don't look like numbers.

2 Comments

Thanks,My theory with the DISTINCT clause was to speed up processing the IN condition. Is the DISTINCT likely to take more time than it saves?
@ChootsMagoots . . . It is immaterial. Let the compiler decide the best approach for handling the IN.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.