0

Is there a way to use a where clause to check if there were zero matches between tables for a record from the first table, and produce one row or results reflecting that?

I'm trying to get results that look like this:

+----------+----------+-----------+----------+-------------+
|                        Results                           |
+----------+----------+-----------+----------+-------------+
| Date     | Queue ID | From Date | To Date  | Campaign ID |
| 3/1/2014 | 1        | 2/24/2014 | 3/2/2014 | 1           |
| 3/1/2014 | 2        | (NULL)    | (NULL)   | (NULL)      |
+----------+----------+-----------+----------+-------------+

From a combination of tables that look like this:

+----------+-------+  +-------+----+  +----+-----------+-----------+----------+
|     Table 1      |  |  Table 2   |  |                Table 3                |
+----------+-------+  +-------+----+  +----+-----------+-----------+----------+
|   Date   | Queue |  | Queue | SP |  | SP | From Date | To Date   | Campaign |
|          |  ID   |  |  ID   | ID |  | ID |           |           |    ID    |
+----------+-------+  +-------+----+  +----+-----------+-----------+----------+
| 3/1/2014 | 1     |  | 1     | 1  |  | 1  | 2/24/2014 | 3/2/2014  | 1        |
| 3/1/2014 | 2     |  | 1     | 2  |  | 2  | 3/3/2014  | 3/9/2014  | 5        |
|          |       |  | 1     | 3  |  | 3  | 3/10/2014 | 3/16/2014 | 1        |
|          |       |  | 1     | 4  |  | 4  | 3/17/2014 | 3/23/2014 | 1        |
|          |       |  | 1     | 5  |  | 5  | 3/24/2014 | 3/30/2014 | 4        |
|          |       |  | 2     | 6  |  | 6  | 3/3/2014  | 3/9/2014  | 5        |
|          |       |  | 2     | 7  |  | 7  | 3/10/2014 | 3/16/2014 | 5        |
|          |       |  | 2     | 8  |  | 8  | 3/17/2014 | 3/23/2014 | 5        |
|          |       |  | 2     | 9  |  | 9  | 3/24/2014 | 3/30/2014 | 5        |
+----------+-------+  +-------+----+  +----+-----------+-----------+----------+


I'm joining Table 1 to Table 2 on QUEUE ID,
and Table 2 to Table 3 on SP ID,
and DATE from Table 1 should fall between Table 3's FROM DATE and TO DATE.

I want a single record returned for each queue, including if there were no date matches. Unfortunately any combinations of joins or where clauses I've tried so far only result in either one record for Queue ID 1 or multiple records for each Queue ID.

4 Answers 4

0

I would suggest this:

SELECT 
t1.Date,
t1.QueueID,
s.FromDate,
s.ToDate,
s.CampaignID
FROM 
    Table1 t1
     LEFT JOIN 
     (
        SELECT 
            t2.QueueID,
            t3.FromDate,
            t3.ToDate,
            t3.CampaignID
        FROM
            Table2 t2 
             INNER JOIN 
            Table3 t3 ON 
                t2.SPID = t3.SPID 
    ) s ON 
        t1.QueueID = s.QueueID AND
        t1.Date BETWEEN s.FromDate AND s.ToDate

SQL Fiddle here with an abbreviated dataset

1
  • That's perfect thank you! I had never built a virtual table within a query before like that, that is great. Apr 28, 2014 at 18:48
0

A trivial amendment to AHiggins code. Using the CTE makes it a little easier to read perhaps.

With AllDates as

(

SELECT 

    t2.QueueID,
    t3.FromDate,
    t3.ToDate,
    t3.CampaignID

FROM Table2 t2 

INNER JOIN Table3 t3 ON

t2.SPID = t3.SPID 

)

SELECT

     t1.Date,
     t1.QueueID,
     s.FromDate,
     s.ToDate,
     s.CampaignID

FROM Table1 t1

LEFT JOIN AllDates s ON

t1.QueueID = s.QueueID AND

t1.Date BETWEEN s.FromDate AND s.ToDate

-1

You want something like:

 select distinct t1.date, t1,queue_id IFNULL(t3.from_date,'NULL'), 
        IFNULL(t3.to_date,'NULL'), IFNULL(t3.campaign,'NULL')
 FROM table1 t1
 LEFT OUTER JOIN table2 t2 on t1.queue_id = t2.queue_id
 left outer join table3 t3 on t2.sp_id = t3.sp_id
 where t3.from_date <= t1.date
 AND t3.to_date >= t1.date

This will select dsitinct records from the table (eliminating null duplicates and replacing them with NULL)

3
  • Thank you. I don't know why, but adding DISTINCT doesn't seem to have any effect on my results in this case. With this query I still get a full list of records where the table 1.date is outside of the table 3.to and from dates, which is kind of the opposite of what I want. Apr 28, 2014 at 18:15
  • I had the signs wrong from the dates, sorry. Those filters would not have done anything Apr 28, 2014 at 18:17
  • You didn't correct the errors in the code you copied from Jonathan: t1,queue_id t3.from_date; the >= and <= are reversed, and the requirement included the display of the NULL values.
    – Joe
    Apr 28, 2014 at 18:17
-1
SELECT t1.[Date], t1.[Queue ID], s.[From Date], s.[To Date], s.[Campaign ID]
  FROM table1 t1
    LEFT JOIN (SELECT t3.*, t2.[Queue ID] FROM table3 t3 JOIN table2 t2 ON t2.[SP ID] = t3.[SP ID]) s
      ON s.[Queue ID] = t1.[Queue ID] AND t1.[Date] BETWEEN s.[From Date] AND s.[To Date]

SQL Fiddle

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.