TSQL - FROM 子查询中的 TOP X?

发布于 2024-08-31 22:06:57 字数 151 浏览 4 评论 0原文

有人可以告诉我一种过滤位于 FROM 子句中的子查询的方法吗? 我希望它看起来像这样:

SELECT *
FROM TABLE_A
LEFT JOIN (TOP 8 TABLE_B) ON TABLE_B.id = TABLE_A.id

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

入画浅相思 2024-09-07 22:06:57

如果您需要关联子查询,那么您需要使用 APPLY 而不是JOIN:

SELECT *
FROM TABLE_A
CROSS APPLY (
 SELECT TOP (8) *
 FROM TABLE_B
 WHERE TABLE_B.id = TABLE_A.id
 ORDER BY ...) AS B;

这将为您提供 A 中每行的 B 前 8 行。我看到发布的其他解决方案将为您提供 A 和全局 TOP 8 之间的 JOIN从B

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

够运 2024-09-07 22:06:57
SELECT * 
FROM TABLE_A AS a
LEFT JOIN (SELECT TOP 8 id, field1, field2
           FROM TABLE_b) AS b
    ON a.id = b.id

应该有效。

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.

彩虹直至黑白 2024-09-07 22:06:57
SELECT *
FROM TableA
LEFT JOIN ( SELECT TOP 8 * FROM TableB) B
   ON B.id=TableA.id
SELECT *
FROM TableA
LEFT JOIN ( SELECT TOP 8 * FROM TableB) B
   ON B.id=TableA.id
情独悲 2024-09-07 22:06:57

请尝试以下操作:

SELECT 
column_names 
FROM 
TABLE_A A LEFT JOIN (SELECT TOP 8 column_names FROM TABLE_B) as B
on A.Id=B.ID

注意事项:

不要使用 *,因为它会导致性能限制。

如果您只关心 ID,则只从 Table_B

HTH获取 ID

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

您可以考虑采用不同的方法,例如:

SELECT * FROM TABLE_A WHERE TABLE_A.ID IN ( SELECT TOP 8 ID FROM TABLE_B )

You might consider a different approach such as:

SELECT * FROM TABLE_A WHERE TABLE_A.ID IN ( SELECT TOP 8 ID FROM TABLE_B )
星星的軌跡 2024-09-07 22:06:57

您可以使用 ORDER BY,甚至使 TOP N 使用变量:

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 基于 OP 注释:

问题是 TOP 8
表不包含我的ID
我正在主查询中进行过滤。

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

输出:

rowid       rowid       RowNumber
----------- ----------- --------------------
6           6           1
7           7           2

(2 row(s) affected)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文