sql-server:将这两个查询结果合并为1

时间:2014-06-17 19:46:08

标签: sql-server

我有两个创建排名列表的查询然后我从结果中选择一个特定的行,因此两个查询都返回1个值。我如何加入结果,以便将它们一起返回。有这个逻辑的东西:

SELECT x.a, y.b FROM (SELECT * from a) as x, (SELECT * FROM b) as y

query1

declare @MainHospital varchar(50)='Hospital1';

                               WITH cte AS (
                               SELECT 4 score, growth1 area FROM Survey
                               WHERE MainHospital = @MainHospital
                               UNION ALL
                               SELECT 3 score, growth2 area FROM Survey
                               WHERE MainHospital = @MainHospital
                               UNION ALL
                               SELECT 2 score, growth3 area FROM Survey
                               WHERE MainHospital = @MainHospital
                               UNION ALL
                               SELECT 1 score, growth4 area FROM Survey
                               WHERE MainHospital = @MainHospital
                               UNION ALL
                               SELECT 0 score, growth5 area FROM Survey
                               WHERE MainHospital = @MainHospital
                               ),

                                cte2 AS (
                               SELECT area, SUM(score) score FROM cte
                               GROUP BY area
                               HAVING area IS NOT NULL                                   
                               ),

                                cte3 AS (
                               SELECT area, score, 
                               Row_Number() OVER (ORDER BY score DESC) row,
                               RANK() OVER (ORDER BY score DESC) rank
                               FROM cte2
                               )                                   

                               SELECT top 1 area AS Growth
                               FROM cte3
                               WHERE row = 1

QUERY2

                               WITH Ecte AS (
                               SELECT 4 score, Efficiency1 area FROM Survey
                               WHERE MainHospital = @MainHospital
                               UNION ALL
                               SELECT 3 score, Efficiency2 area FROM Survey
                               WHERE MainHospital = @MainHospital
                               UNION ALL
                               SELECT 2 score, Efficiency3 area FROM Survey
                               WHERE MainHospital = @MainHospital
                               UNION ALL
                               SELECT 1 score, Efficiency4 area FROM Survey
                               WHERE MainHospital = @MainHospital
                               UNION ALL
                               SELECT 0 score, Efficiency5 area FROM Survey
                               WHERE MainHospital = @MainHospital
                               ), Ecte2 AS (
                               SELECT area, SUM(score) score FROM Ecte
                               GROUP BY area
                               HAVING area IS NOT NULL                                  

                               ), Ecte3 AS (
                               SELECT area, score, 
                               Row_Number() OVER (ORDER BY score DESC) row,
                               RANK() OVER (ORDER BY score DESC) rank
                               FROM Ecte2
                               )
                               SELECT TOP 1 area as Efficiency
                               FROM Ecte3
                               WHERE row = 1

1 个答案:

答案 0 :(得分:0)

您是否考虑过UNION / UNION ALL?您必须在每个回报中使用相同的列数才能使其生效。

例如:

SELECT COL1, COL2, COL3 FROM TABLE1
UNION ALL
SELECT COL1, COL2, COL3 FROM TABLE2

对于CTE,请执行以下操作:

;WITH CTE1 (col1, col2)
AS
(
     SELECT col1, col2
     FROM ...
)
, CTE2 (col1, col2)
AS
(
     SELECT col1, col2
     FROM ....
)

SELECT col1, col2
FROM CTE1
WHERE something = true
UNION ALL
SELECT col1, col2
FROM CTE2
WHERE something = true

注意:UNION ALL将返回重复的行,UNION将删除相同的行,就像在每个选定的列上GROUP BY一样。

您的代码将更改如下:

declare @MainHospital varchar(50)='Hospital1';

WITH cte AS (
SELECT 4 score, growth1 area FROM Survey
WHERE MainHospital = @MainHospital
UNION ALL
SELECT 3 score, growth2 area FROM Survey
WHERE MainHospital = @MainHospital
UNION ALL
SELECT 2 score, growth3 area FROM Survey
WHERE MainHospital = @MainHospital
UNION ALL
SELECT 1 score, growth4 area FROM Survey
WHERE MainHospital = @MainHospital
UNION ALL
SELECT 0 score, growth5 area FROM Survey
WHERE MainHospital = @MainHospital
),

cte2 AS (
SELECT area, SUM(score) score FROM cte
GROUP BY area
HAVING area IS NOT NULL                                   
),

cte3 AS (
SELECT area, score, 
Row_Number() OVER (ORDER BY score DESC) row,
RANK() OVER (ORDER BY score DESC) rank
FROM cte2
),                                 

Ecte AS (
SELECT 4 score, Efficiency1 area FROM Survey
WHERE MainHospital = @MainHospital
UNION ALL
SELECT 3 score, Efficiency2 area FROM Survey
WHERE MainHospital = @MainHospital
UNION ALL
SELECT 2 score, Efficiency3 area FROM Survey
WHERE MainHospital = @MainHospital
UNION ALL
SELECT 1 score, Efficiency4 area FROM Survey
WHERE MainHospital = @MainHospital
UNION ALL
SELECT 0 score, Efficiency5 area FROM Survey
WHERE MainHospital = @MainHospital
), Ecte2 AS (
SELECT area, SUM(score) score FROM Ecte
GROUP BY area
HAVING area IS NOT NULL                                  

), Ecte3 AS (
SELECT area, score, 
Row_Number() OVER (ORDER BY score DESC) row,
RANK() OVER (ORDER BY score DESC) rank
FROM Ecte2
)

SELECT TOP 1 area as Efficiency
FROM Ecte3
WHERE row = 1                              
UNION ALL
SELECT top 1 area AS Growth
FROM cte3
WHERE row = 1

如果您希望将两个值并排,请将最终查询更改为:

SELECT TOP 1 T1.area as Efficiency, T2.area AS Growth
FROM Ecte3 T1, cte3 T2
WHERE T1.row = 1 AND T2.row = 1