查询上一个查询的结果集 [英] Querying the Result set of a Previous Query
问题描述
我有一个查询,例如Query1 = Select Name from table where some Criteria
.现在这个查询当然返回一个结果集,我想要的是查询这个查询的结果集,例如我只想要上面查询的唯一名称select Distinct(Name) from Query1
.我应该提到我知道我可以在 Query1 中使用 distinct
但这只是一个例子,我的真实场景有些不同,我想知道是否可以查询以前的结果集询问.我使用的是 SQL Server 2012.
I have a query for example Query1 = Select Name from table where some Criteria
.
Now this query returns a result set of course, what I want is to query the result set of this query, for example I only want the unique Names from the above query select Distinct(Name) from Query1
. I should mention that I know I can just use distinct
in Query1 but this is just an example my real scenario is somewhat different, what I want to know is whether it's possible to query the result set of a previous query.
I am using SQL Server 2012.
推荐答案
您可以使用 WITH
子句
WITH SomeClients AS (
SELECT
c.ID
FROM Clients c
WHERE c.Name LIKE '%hello%'
)
SELECT DISTINCT
sc.ID
FROM SomeClients sc
这篇关于查询上一个查询的结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!