[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

LEFT OUTER JOIN That Doesn't!

Ross Culver

3/20/2007 4:03:00 PM

Here's a simple query:

SELECT TOP (100) PERCENT I.IBITNO, I.IBITCL, I.IBITSC, I.OHQ, I.AvgCost, ISNULL(dbo.fnConvertDate(H.IATRDT), '2001-12-31') AS TDate, ISNULL(H.IATRQT, 0) AS IATRQT
FROM dbo.vwInventoryAgingI AS I LEFT OUTER JOIN

dbo.IAHST AS H ON LTRIM(RTRIM(I.IBITNO)) = LTRIM(RTRIM(H.IAITNO))

WHERE (H.IATRCD = 'R') and I.IBITCL = '90'

ORDER BY I.IBITNO, H.IATRDT DESC

The left-hand side should return 3483 records with only 597 matching records on the right-hand side, but instead, the query only returns the 597 records.

Why all 3483 records not returned with the 597 corresponding fields and 2886 records with the substitute for null values? (Using SQL 2005)

Note: I've tried taking out the order by and top 100%, but that didn't make any difference.

Thanks, Ross.
3 Answers

markc600

3/20/2007 4:17:00 PM

0

Try changing

WHERE (H.IATRCD = 'R') and I.IBITCL = '90'

to

AND (H.IATRCD = 'R') and I.IBITCL = '90'


Roy Harvey

3/20/2007 4:19:00 PM

0

The test WHERE (H.IATRCD = 'R') is eliminating the OUTER cases, as the
NULLs for the OUTER rows do not pass the test.

The usual approach is to move this test to the ON clause of that
table's OUTER JOIN.

Also, TOP (100) PERCENT is a bad idea and should be removed. It does
not change the results, and the one purpose it ever had was an
unreliable, undocumented and unsupported way to try to "order" a view
that completely broke down in SQL Server 2005.

SELECT I.IBITNO, I.IBITCL, I.IBITSC,
I.OHQ, I.AvgCost,
ISNULL(dbo.fnConvertDate(H.IATRDT), '2001-12-31') AS TDate,
ISNULL(H.IATRQT, 0) AS IATRQT
FROM dbo.vwInventoryAgingI AS I
LEFT OUTER
JOIN dbo.IAHST AS H
ON LTRIM(RTRIM(I.IBITNO)) = LTRIM(RTRIM(H.IAITNO))
AND (H.IATRCD = 'R')
WHERE I.IBITCL = '90'
ORDER BY I.IBITNO, H.IATRDT DESC

Roy Harvey
Beacon Falls, CT

On Tue, 20 Mar 2007 11:03:11 -0500, "Ross Culver"
<rculver@warrenalloy.com> wrote:

>Here's a simple query:
>
>SELECT TOP (100) PERCENT I.IBITNO, I.IBITCL, I.IBITSC, I.OHQ, I.AvgCost, ISNULL(dbo.fnConvertDate(H.IATRDT), '2001-12-31') AS TDate, ISNULL(H.IATRQT, 0) AS IATRQT
>FROM dbo.vwInventoryAgingI AS I LEFT OUTER JOIN
>
>dbo.IAHST AS H ON LTRIM(RTRIM(I.IBITNO)) = LTRIM(RTRIM(H.IAITNO))
>
>WHERE (H.IATRCD = 'R') and I.IBITCL = '90'
>
>ORDER BY I.IBITNO, H.IATRDT DESC
>
>The left-hand side should return 3483 records with only 597 matching records on the right-hand side, but instead, the query only returns the 597 records.
>
>Why all 3483 records not returned with the 597 corresponding fields and 2886 records with the substitute for null values? (Using SQL 2005)
>
>Note: I've tried taking out the order by and top 100%, but that didn't make any difference.
>
>Thanks, Ross.

Ross Culver

3/20/2007 4:38:00 PM

0

Thanks to both you and Mark. I can't tell you how much appreciate your
responses! And you were both right!


"Roy Harvey" <roy_harvey@snet.net> wrote in message
news:c12003dquuvstkk9a90jdmn6frd2pi0qkc@4ax.com...
> The test WHERE (H.IATRCD = 'R') is eliminating the OUTER cases, as the
> NULLs for the OUTER rows do not pass the test.
>
> The usual approach is to move this test to the ON clause of that
> table's OUTER JOIN.
>
> Also, TOP (100) PERCENT is a bad idea and should be removed. It does
> not change the results, and the one purpose it ever had was an
> unreliable, undocumented and unsupported way to try to "order" a view
> that completely broke down in SQL Server 2005.
>
> SELECT I.IBITNO, I.IBITCL, I.IBITSC,
> I.OHQ, I.AvgCost,
> ISNULL(dbo.fnConvertDate(H.IATRDT), '2001-12-31') AS TDate,
> ISNULL(H.IATRQT, 0) AS IATRQT
> FROM dbo.vwInventoryAgingI AS I
> LEFT OUTER
> JOIN dbo.IAHST AS H
> ON LTRIM(RTRIM(I.IBITNO)) = LTRIM(RTRIM(H.IAITNO))
> AND (H.IATRCD = 'R')
> WHERE I.IBITCL = '90'
> ORDER BY I.IBITNO, H.IATRDT DESC
>
> Roy Harvey
> Beacon Falls, CT
>
> On Tue, 20 Mar 2007 11:03:11 -0500, "Ross Culver"
> <rculver@warrenalloy.com> wrote:
>
>>Here's a simple query:
>>
>>SELECT TOP (100) PERCENT I.IBITNO, I.IBITCL, I.IBITSC, I.OHQ, I.AvgCost,
>>ISNULL(dbo.fnConvertDate(H.IATRDT), '2001-12-31') AS TDate,
>>ISNULL(H.IATRQT, 0) AS IATRQT
>>FROM dbo.vwInventoryAgingI AS I LEFT OUTER JOIN
>>
>>dbo.IAHST AS H ON LTRIM(RTRIM(I.IBITNO)) = LTRIM(RTRIM(H.IAITNO))
>>
>>WHERE (H.IATRCD = 'R') and I.IBITCL = '90'
>>
>>ORDER BY I.IBITNO, H.IATRDT DESC
>>
>>The left-hand side should return 3483 records with only 597 matching
>>records on the right-hand side, but instead, the query only returns the
>>597 records.
>>
>>Why all 3483 records not returned with the 597 corresponding fields and
>>2886 records with the substitute for null values? (Using SQL 2005)
>>
>>Note: I've tried taking out the order by and top 100%, but that didn't
>>make any difference.
>>
>>Thanks, Ross.