我有一个简单的存储过程,其中包含两个与联合连接的查询:
I have a simple stored proc with two queries joined with a union:
select name as 'result'
from product
where...
union
select productNum as 'result'
from product
where...
我想将此限制在前 10 个结果中.
I want to limit this to the TOP 10 results.
如果我将 TOP 10 放在每个单独的查询中,我总共会得到 20 个结果.
If I put TOP 10 in each seperate query I get 20 results total.
将总结果限制为 10 的最有效方法是什么?我不想在每个都做前 5 名,因为我可能最终会遇到类似 7 个名字"的情况.和 3 个产品编号".
What is the most efficient way to limit total results to 10? I dont want to do TOP 5 in each because I may end up in a situation where I have something like 7 "names" and 3 "productsNumbers".
WITH Results (Result)
AS
(
select name as 'result'
from product
where...
union
select productNum as 'result'
from product
where...
)
SELECT TOP 10 * FROM Results
常用表表达式
这篇关于限制从联合查询返回的行的最有效方法 - TSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
将每个子标记转换为具有多个分隔符的单列-SQLConverting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(将每个子标记转换为具有多个分隔符的单列-SQ
如何从多个表创建视图?How can I create a view from more than one table?(如何从多个表创建视图?)
根据前一行内的计算值创建计算值Create calculated value based on calculated value inside previous row(根据前一行内的计算值创建计算值)
如何将表格的前两列堆叠成一列,但也仅将第三How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何将表格的前两列堆
递归 t-sql 查询Recursive t-sql query(递归 t-sql 查询)
将月份名称转换为日期/月份编号(问题和答案的组Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(将月份名称转换为日期/月份编号(问题和答案的组合