在SQL中获取group by的计数总和

时间:2017-03-08 09:17:20

标签: sql sql-server

我试图将数据作为基于组的总和呈现,并且预期输出类似于

  Windows-10  yyy 
  Windows-8   xxx
  Windows-7   abc 

在表LOGINHISTORYTABLE中,我有一个列useragent,它以特殊格式存储操作系统和浏览器的值,如

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/8.0; 

Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36   etc

所以我的计划是删除此列中的所有空格,以便值变为

Mozilla/4.0(compatible;MSIE7.0;WindowsNT10.0;WOW64;Trident/8.0;
Mozilla/5.0(WindowsNT6.3;Trident/7.0;Touch;MAARJS;rv:11.0)likeGecko  etc

并假设该列包含WindowsNT10.0,我将其视为Windows 10实例

如果存在WindowsNT6.3,我将其视为Windows 8实例等

所以我能够通过这个查询找到计数:

    SELECT 
        COUNT(d.UserAgent) as countname,
        Replace(d.UserAgent, ' ', ' ') as name
    FROM 
        LOGINHISTORYTABLE as d  
    WHERE 
        d.CreatedDate > '2017-02-08' 
        AND d.CreatedDate < '2017-02-09' 
        AND (Replace(d.UserAgent, ' ', '') like '%WindowsNT10.0%'
        OR Replace(d.UserAgent, ' ', '') like '%WindowsNT6.3%')
    GROUP BY 
        d.UserAgent

现在我要为每个组找到SUM

我试过

    SELECT SUM(countname) as TotalCount , 'Windows 10' as OS
    FROM
    (
        SELECT COUNT(d.UserAgent) as countname,
            Replace(d.UserAgent, ' ', ' ') as name
        FROM LOGINHISTORYTABLE as d  
        WHERE d.CreatedDate>  '2017-02-08' AND d.CreatedDate < '2017-02-09' AND 
           (Replace(d.UserAgent, ' ', '') like '%WindowsNT10.0%'
           OR 
           Replace(d.UserAgent, ' ', '') like '%WindowsNT6.3%')
        GROUP BY d.UserAgent
    )a

但这仅返回总和。如何将此总和作为分组,如果可能,我如何使用每个组的自定义标签

3 个答案:

答案 0 :(得分:2)

尝试

WITH TranslatedOS_CTE AS (
SELECT  UserAgent,CreatedDate,
        case when UserAgent like '%NT 6.3%' then 'Windows8'
        when UserAgent like '%Windows NT 10.0%' then 'Windows10'
        else UserAgent
        end as OS        
        FROM LOGINHISTORYTABLE 
)

Select Count(0), [OS] from TranslatedOS_CTE
WHERE CreatedDate>  '2017-02-08' AND CreatedDate < '2017-02-09'
group by OS

答案 1 :(得分:1)

您应该在外部查询中对名称进行分组,但首先应构建将UserAgent替换为OS的案例或正则表达式

 SELECT SUM(countname) as TotalCount , 
        name
    FROM
    (
        SELECT COUNT(d.UserAgent) as countname,
            Replace(d.UserAgent, ' ', ' ') as name
        FROM LOGINHISTORYTABLE as d  
        WHERE d.CreatedDate>  '2017-02-08' AND d.CreatedDate < '2017-02-09' AND 
           (Replace(d.UserAgent, ' ', '') like '%WindowsNT10.0%'
           OR 
           Replace(d.UserAgent, ' ', '') like '%WindowsNT6.3%')
        GROUP BY d.UserAgent
    )a
group by name

答案 2 :(得分:0)

这是你在找什么?

SELECT name as OS,SUM(countname) as TotalCount
    FROM
    (
        SELECT COUNT(d.UserAgent) as countname,
            Replace(d.UserAgent, ' ', ' ') as name
        FROM LOGINHISTORYTABLE as d  
        WHERE d.CreatedDate>  '2017-02-08' AND d.CreatedDate < '2017-02-09' AND 
           (Replace(d.UserAgent, ' ', '') like '%WindowsNT10.0%'
           OR 
           Replace(d.UserAgent, ' ', '') like '%WindowsNT6.3%')
        GROUP BY d.UserAgent
    )a
    GROUP BY name