views:

51

answers:

2

We have a feature that allows us to create SQL to get back data from one table based on a nested query that filters records based on matching criteria from another table. Now, we need to be able to get back data from the first table based on the top x records of the nexted query, rather than all matching records. For Example we want something like

SELECT Name, Address, City, State, Zip 
  FROM CUSTOMERS
 WHERE Customer_Location IN (SELECT TOP 100 
                                    CustomerID, 
                                    Rank() OVER PARTITION BY TID ORDER BY TerritoryName DESC) AS 'RANK'
                               FROM Territories
                              WHERE Nation = 'Canada')

But using the IN statement, so far, is no good because we can only return one column, and using the EXISTS isn't working as all the exists does is return "TRUE" if any fields match (even when we put a link back to the main query). Does anyone know how I can get this to work? Thanks.

+2  A: 

You want to join to your nested query. I've made an assumption on the appropriate join condition, but it would be something like:

SELECT Name, Address, City, State, Zip 
FROM CUSTOMERS C
INNER JOIN (SELECT TOP 100 CustomerID, TerritoryName, 
            Rank() OVER PARTITION BY TID ORDER BY TerritoryName DESC) AS 'RANK'
            FROM Territories
            WHERE Nation = 'Canada') T
ON C.Customer_Location = T.TerritoryName
Joe Stefanelli
+1: Faster than me
OMG Ponies
This works very well. Thanks.
Tom
A: 

I'm not sure about the join condition either. It seems unexpected to match on Customer_Location and CustomerID. Maybe you can clarify?

I think you would need a CTE or derived table.

WITH R AS
(
SELECT CustomerID, 
Rank() OVER (PARTITION BY TID ORDER BY TerritoryName DESC) AS [rank]
FROM Territories
WHERE Nation = 'Canada'
)


SELECT Name, Address, City, State, Zip 
  FROM CUSTOMERS
 WHERE Customer_Location IN (SELECT CustomerID FROM R WHERE [rank] <= 100)
Martin Smith
The main thing about the SQL I posted above is that is an example of the type of queries we'd be running. The problem with the IN statement is that in SQL Server, I can only have the secondary query (inside the parentheses for the IN) return a single column, so in the case of the SQL I included, it wouldn't work, and an EXISTS would essentially give carte blanche to ignore the secondary query unless it returned an empty set in which case, the first query would return an empty set.
Tom
He wants to pull more columns from Territories than CustomerID... your query doesn't offer this.
Emtucifor
@Emtucifor - It would be trivial to convert it to a JOIN. It was based on the OP's code which I had already queried in a comment.
Martin Smith
His query does appear to have some problems (I saw this too) but he still listed as a requirement to pull multiple columns, and apparently switching it to a JOIN is not so trivial for him, so he was relying on you to show him how to do this! :)
Emtucifor
@Emtucifor Yep fair enough. I won't bother though as the OP seems to have a solution they are happy with.
Martin Smith