如何在SQL中使用具有order by和count()的两个查询的并集

时间:2016-02-12 05:25:44

标签: sql sql-server union

我有两个查询如何使用union以便在单个查询执行中查看两个结果

select TOP 1 AGE, DIAGNOSIS_CODE_1, count(DIAGNOSIS_CODE_1) as total_count
from Health
where age = 7
group by AGE, DIAGNOSIS_CODE_1
order by total_count DESC;

select TOP 1 AGE, DIAGNOSIS_CODE_1, count(DIAGNOSIS_CODE_1) as total_count
from Health
where age = 9
group by AGE, DIAGNOSIS_CODE_1
order by total_count DESC;

示例输出 enter image description here

示例输出 enter image description here

5 个答案:

答案 0 :(得分:2)

您可以row_number() over(partition by..之类的方式执行此操作,

select
    AGE, 
    DIAGNOSIS_CODE_1, 
    total_count
from (
    select  
        AGE, 
        DIAGNOSIS_CODE_1, 
        count(DIAGNOSIS_CODE_1) as total_count,
        row_number() over (partition by AGE order by count(DIAGNOSIS_CODE_1) desc) rnk
    from Health
    where age in (7, 9)
    group by AGE, DIAGNOSIS_CODE_1
) x
where rnk = 1

或者您可以使用union all之类的;

with tmp_1 as (
    select TOP 1 AGE, DIAGNOSIS_CODE_1, count(DIAGNOSIS_CODE_1) as total_count
    from Health
    where age = 7
    group by AGE, DIAGNOSIS_CODE_1
    order by total_count DESC
),
tmp_2 as (
    select TOP 1 AGE, DIAGNOSIS_CODE_1, count(DIAGNOSIS_CODE_1) as total_count
    from Health
    where age = 9
    group by AGE, DIAGNOSIS_CODE_1
    order by total_count DESC
)
select AGE, DIAGNOSIS_CODE_1, total_count from tmp_1
union all
select AGE, DIAGNOSIS_CODE_1, total_count from tmp_2

答案 1 :(得分:1)

只需在这些查询之间添加UNION ALL即可。应用UNION ALL时,ORDER BY子句不会接受。所以我把它们放在一个内在的集合中来结束它。

    SELECT * FROM (

    SELECT TOP 1 AGE, DIAGNOSIS_CODE_1, COUNT(DIAGNOSIS_CODE_1) AS TOTAL_COUNT
    FROM HEALTH
    WHERE AGE = 7
    GROUP BY AGE, DIAGNOSIS_CODE_1


    UNION ALL

    SELECT TOP 1 AGE, DIAGNOSIS_CODE_1, COUNT(DIAGNOSIS_CODE_1) AS TOTAL_COUNT
    FROM HEALTH
    WHERE AGE = 9
    GROUP BY AGE, DIAGNOSIS_CODE_1

    )AS A
    ORDER BY TOTAL_COUNT DESC;

根据情况你可以这样做。如果您的情况是单独下订单,那么您可以在内部集合中进行订购。

    SELECT * FROM (

    SELECT TOP 1 AGE, DIAGNOSIS_CODE_1, COUNT(DIAGNOSIS_CODE_1) AS TOTAL_COUNT
    FROM HEALTH
    WHERE AGE = 7
    GROUP BY AGE, DIAGNOSIS_CODE_1
    ORDER BY TOTAL_COUNT DESC;
    )AS B

    UNION ALL

    SELECT * FROM (
    SELECT TOP 1 AGE, DIAGNOSIS_CODE_1, COUNT(DIAGNOSIS_CODE_1) AS TOTAL_COUNT
    FROM HEALTH
    WHERE AGE = 9
    GROUP BY AGE, DIAGNOSIS_CODE_1
    ORDER BY TOTAL_COUNT DESC;
    )AS A

答案 2 :(得分:0)

如果您想使用union,可以尝试以下查询。

(select TOP 1 AGE, DIAGNOSIS_CODE_1, count(DIAGNOSIS_CODE_1) as total_count
from Health
where age = 7
group by AGE, DIAGNOSIS_CODE_1 ) union
(select TOP 1 AGE, DIAGNOSIS_CODE_1, count(DIAGNOSIS_CODE_1) as total_count
from Health
where age = 9
group by AGE, DIAGNOSIS_CODE_1) order by total_count DESC; 

对于这个特定的查询,我建议你使用where age IN (7,9)来减少工作量。

答案 3 :(得分:0)

当UNION语句中的任何SELECT语句包含ORDER BY子句时,该子句应放在所有SELECT语句之后。Using UNION of two SELECT statements with ORDER BY

SELECT * FROM (
                      SELECT TOP 1 AGE, DIAGNOSIS_CODE_1, count(DIAGNOSIS_CODE_1) as total_count
                      FROM Health
                      WHERE Age = 7
                      GROUP BY AGE, DIAGNOSIS_CODE_1

                     UNION ALL    

                      SELECT TOP 1 AGE, DIAGNOSIS_CODE_1, count(DIAGNOSIS_CODE_1) as total_count
                      FRPM Health
                      WHERE Age = 9
                      GROUP BY AGE, DIAGNOSIS_CODE_1
) AS T

ORDER BY total_count DESC;

答案 4 :(得分:0)

你必须使用这样的东西。我没有使用工会,而是计算了2个不同的年龄组,然后选择其中的最大数量

SQLFiddle Demo

SELECT t1.age, 
       t1.diagnosis_code_1, 
       t1.total_count 
FROM   (SELECT t.*, 
               Rank() 
                 OVER ( 
                   partition BY age 
                   ORDER BY total_count DESC) AS tc 
        FROM   (SELECT age, 
                       diagnosis_code_1, 
                       Count(diagnosis_code_1) AS total_count 
                FROM   health 
                WHERE  age IN ( 7, 9 ) 
                GROUP  BY age, 
                          diagnosis_code_1
                ) t
        ) t1 
WHERE  t1.tc = 1 

您可以使用相同的查询并修改或删除WHERE age IN ( 7, 9 )子句,以便为更多count组获取age