SQL Server:如何在单个查询中执行2个select语句

时间:2016-05-31 05:46:29

标签: sql sql-server

我想首先计算列:作为每列的实例总数,第二次计算基于条件的实例总数。

我正在使用它:

SELECT
    group_name,
    Definition_Range,
    COUNT([group_name]) AS Number_of_Clients
FROM 
    [Computer Status]
WHERE
    Definition_Range = ' 0-10 Days'
GROUP BY
    group_name,
    (SELECT COUNT([group_name]) AS Total_Clients
     FROM [Computer Status]
     GROUP BY group_name)

1 个答案:

答案 0 :(得分:0)

以下查询没有任何意义

SELECT COUNT([group_name]) AS Total_Clients
         FROM [Computer Status]
         GROUP BY group_name

它返回与主查询中写入的Number_of_Clients相同的内容。 因为您正在group by进行group_name

  

以下是完美查询

   SELECT
        group_name,
        Definition_Range,
        COUNT([group_name]) AS Number_of_Clients
    FROM 
        [Computer Status]
    WHERE
        Definition_Range = ' 0-10 Days'
    GROUP BY
        group_name,Definition_Range