按位置和操作系统类型对数据进行分组

时间:2018-01-26 13:37:23

标签: sql group-by

我有一个返回以下结果的查询:

    Location      OS         Count
    AB02        Windows 7     42
    AB02        Windows 7     2
    AB02        Windows 7     1
    AB02 - SW   Windows 7     36
    AB02 - SW   Windows 10    62

我如何将这些数据合并以获得以下内容?

    Location      OS         Count
    AB02        Windows 7     81
    AB02 - SW   Windows 10    62

我尝试了GROUP BY LEFT(位置,4),操作系统,但这并没有返回我需要的东西。我需要结合操作系统和位置,AB02和AB - SW结合起来。

这是我的问题:

    SELECT MIN(Location) AS Location
    , CASE WHEN OS LIKE '%Windows 7%' THEN 'Windows 7' 
    WHEN OS LIKE '%Windows 10%' THEN 'Windows 10'
    ELSE OS END AS OS
    , COUNT(OS) AS 'Count'          
    FROM ComputerProperties
    GROUP BY LEFT(Location, 4), OS
    ORDER BY Location

1 个答案:

答案 0 :(得分:1)

试试这个

select OS, sum(Count) as count 

FROM
(
     your existing query
) 
as x

group by OS