Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Can this be done with a JOIN?
Message
From
12/04/2007 15:28:29
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2000
Miscellaneous
Thread ID:
01215163
Message ID:
01215185
Views:
17
>>>>I have two tables - we'll call them Customer and Bill. A customer can have zero or many bills, and it is linked by the CustomerID. What I want to do is pull a customer by CustomerID and also pull their last bill only if it is within 30 days old. If there isn't a bill that fits that criteria, I want null value.
>>>>
>>>>Can I do this with a JOIN? Anybody have any advice on how to do the filtering in the JOIN statement?
>>>
>>>Try
>>>
>>>SELECT *
>>>	FROM Customer c
>>>		LEFT JOIN (SELECT TOP 1 FROM Bill b
>>> 			WHERE CustomerID = @CustomerID AND BillDate > ...
>>>                              ORDER BY BillDate DESC) bd on bd.CustomerID = c.CustomerID
>>>	WHERE c.CustomerID = @CustomerID
>>>
>>
>>Sergey, I got an error until I put a "*" in "SELECT TOP 1 FROM Bill".
>
>I told ya. :)

You know, for a few minutes I thought Query Analyzer was incorrect!

Thanks for the help, works beautifully.
Very fitting: http://xkcd.com/386/
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform