Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 JOIN problem

Author  Topic 

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2010-11-01 : 09:40:42
Hi,

I have this simple query which returns 10000 rows.


SELECT main.*
FROM dbo.vwMain AS main


I've added a simple LEFT join, but the results are not as expected. I want the same 10000 rows but with the price appended. However this query only returns 7000 rows. If a row doesn't exist in the pricing table I just want NULL to be returned for that field. How can I do this?

Thanks


SELECT main.*, prices.PR_Basic
FROM dbo.vwMain AS main LEFT OUTER JOIN
PUB_CAR.dbo.NVDPrices AS prices ON main.ID = prices.PR_Id
WHERE (prices.PR_EffectiveTo IS NULL)

TimSman
Posting Yak Master

127 Posts

Posted - 2010-11-01 : 09:58:44
Drop the WHERE clause.
Go to Top of Page

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2010-11-01 : 10:02:37
There are multiple rows with the same PR_ID in the prices table, the current one has PR_EffectiveTo set to NULL so I need to filter on this.

Thanks
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-01 : 11:41:14
quote:
Originally posted by Mondeo

Hi,

I have this simple query which returns 10000 rows.


SELECT main.*
FROM dbo.vwMain AS main


I've added a simple LEFT join, but the results are not as expected. I want the same 10000 rows but with the price appended. However this query only returns 7000 rows. If a row doesn't exist in the pricing table I just want NULL to be returned for that field. How can I do this?

Thanks


SELECT main.*, prices.PR_Basic
FROM dbo.vwMain AS main LEFT OUTER JOIN
PUB_CAR.dbo.NVDPrices AS prices ON main.ID = prices.PR_Id AND prices.PR_EffectiveTo IS NULL
WHERE (prices.PR_EffectiveTo IS NULL)






No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -