问题
Can someone please enlighten me to a way to filter a subquery that is located in a FROM clause? I would like it to look something like this:
SELECT *
FROM TABLE_A
LEFT JOIN (TOP 8 TABLE_B) ON TABLE_B.id = TABLE_A.id
回答1:
Please try this:
SELECT
column_names
FROM
TABLE_A A LEFT JOIN (SELECT TOP 8 column_names FROM TABLE_B) as B
on A.Id=B.ID
Considerations:
Do not use * since it would lead to performance constraints.
IF you are concerned about just the ID then get only the ID from Table_B
HTH
回答2:
If you need to correlate the subquery then you need to use APPLY instead of JOIN:
SELECT *
FROM TABLE_A
CROSS APPLY (
SELECT TOP (8) *
FROM TABLE_B
WHERE TABLE_B.id = TABLE_A.id
ORDER BY ...) AS B;
This will give you the top 8 rows from B for each row in A. The other solutions I see posted will give you the JOIN between A and the global TOP 8 from B
回答3:
SELECT *
FROM TABLE_A AS a
LEFT JOIN (SELECT TOP 8 id, field1, field2
FROM TABLE_b) AS b
ON a.id = b.id
Should work.
回答4:
SELECT *
FROM TableA
LEFT JOIN ( SELECT TOP 8 * FROM TableB) B
ON B.id=TableA.id
回答5:
You might consider a different approach such as:
SELECT * FROM TABLE_A WHERE TABLE_A.ID IN ( SELECT TOP 8 ID FROM TABLE_B )
回答6:
you can use an ORDER BY, and even make the TOP N use a variable:
declare @x table (rowid int)
declare @y table (rowid int)
INSERT @x (rowID) SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8
INSERT @y (rowID) SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8
DECLARE @z int
SET @z=2
SELECT
a.*, b.*
FROM @x a
LEFT JOIN (SELECT TOP (@z)
*
FROM @y
ORDER BY rowid
) b ON a.rowid=b.rowid
OUTPUT:
rowid rowid
----------- -----------
1 1
2 2
3 NULL
4 NULL
5 NULL
6 NULL
7 NULL
8 NULL
(8 row(s) affected)
EDIT based on OPs comments:
The problem is that the TOP 8 of that table does not contain the ID that I am filtering in the main query.
declare @x table (rowid int)
declare @y table (rowid int)
INSERT @x (rowID) SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8
INSERT @y (rowID) SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
DECLARE @z int
SET @z=2
SELECT
a.*, b.*
FROM @x a
LEFT JOIN (SELECT
*, ROW_NUMBER() OVER(ORDER BY rowid) AS RowNumber
FROM @y
) b ON a.rowid=b.rowid
WHERE b.RowNumber<=@z
OUTPUT:
rowid rowid RowNumber
----------- ----------- --------------------
6 6 1
7 7 2
(2 row(s) affected)
来源:https://stackoverflow.com/questions/2791415/tsql-top-x-in-from-subquery