简体   繁体   中英

SQL Left Outer Join doesn't give full table

I have searched for similar issues but I found nothing...

I have a problem with joining 2 tables in SQL. First table is created using following procedure

DECLARE @Sequence TABLE (n DATETIME NOT NULL)
DECLARE @Index INT
SET @Index = 1
WHILE @Index <= 96 BEGIN 
    INSERT @Sequence (n) VALUES (DATEADD(Minute, @Index * 5, DATEADD(Hour, -8, '06-25-2010 00:00:00')))
    SET @Index = @Index + 1 
END

And when I run a regular query like such:

SELECT
    Sequence.n
FROM
    @Sequence as Sequence 

I get what I expect - 96 rows with DateTime values spaced by 5 minute intervals, ending 06-25-2010 00:00:00 (this value will later be substituted by parameter in SSRS Report, thats why it might look weird to specify 'end' of the range and use double DATEADD).

Now the second table contains values registered by PLMC controllers, and they also happen to register one record per 5 minutes (per pointID, but not to complicate it lets assume there is one PointID).

The problem is that sometimes the controller goes offline, and for a given point there is no value registered, not even a 0. Thats why if I want to get a full picture of last 8 hours from any given point I came up with this sequence table. So, if I take the values from the other table in the same time range using this query:

SELECT
    DataTime, DataValue
FROM
    PointValue
WHERE
    PointValue.DataTime > DATEADD(Hour, -8, '06-22-2010 00:00:00')
    AND PointValue.DataTime < '06-22-2010 00:00:00'
    AND PointID = '5284'

I will get only 56 rows. This is because after 20:30 on that day the controller went offline and there are no records registered.

So here is the problem. I try to run this query to get one value for every 5 minutes, and hopefully still see the whole 96 rows (8 hours in 5min intervals) with null values after 20:30:

SELECT
    Sequence.n,
    PointValue.DataValue 
FROM
    @Sequence as Sequence LEFT OUTER JOIN PointValue 
    ON Sequence.n = PointValue.DataTime 
WHERE 
    PointID = '5280'

Unforetunately the result is still 56 rows, with the same time stamps as the last query... I have tried every possible join and cannot get the Null values for the last 3,5h of the day. I'm sure I'm making a very simple mistake but I have tried different ways to solve it for hours now and I seriously need help.

SOLVED: Thanks for your comments, I have started to modify the query after reading first comment to appear, by Blorgbeard. All I had to do is make a carthesian multiplication of my time sequence x all relevan pointIDs (since I don't need all), so as a result my 'FROM' looks as follows:

(SELECT Sequence.n, dbo.LABELS.theIndex FROM @TimeSequence as Sequence, dbo.LABELS
WHERE LEFT(dbo.LABELS.theLabel,2)='VA') as BaseTable
LEFT OUTER JOIN PointValue ON BaseTable.n = PointValue.DataTime AND BaseTable.theIndex = PointValue.PointID 

Again, thank you for your help!

You need to move the PointID comparison into the ON clause - with it in the WHERE clause, you're forcing the LEFT JOIN to become an INNER JOIN:

ON Sequence.n = PointValue.DataTime AND
   PointValue.PointID = '5280'

Conditions in the WHERE clause have to be met by every row in the result set.

I think the problem is:

WHERE 
   PointID = '5280'

Because PointID is in the PointValue table, so it will be null for missing rows, and null does not equal '5280'.

I think you could change it like this to make it work:

WHERE 
   (PointID is null) or (PointID = '5280')

This is because, in your NULL rows, PointID is not '5280'.

Try adding that to your JOIN clause...

ON Sequence.n = PointValue.DataTime AND PointID = '5280'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM