我怎样才能在部分字段上进行GROUP BY?

时间:2011-09-07 20:12:44

标签: tsql group-by

您好我的数据库中有一些表,其中包含一些浏览器信息我目前正在使用以下T-SQL来获取摘要列表:

SELECT Browser, Version, SUM(Count) as Count FROM AnalyticsBrowsers
WHERE Month=8 AND Year=2011
AND UserAgent NOT LIKE '%YandexBot%'
AND UserAgent NOT LIKE '%Googlebot%'
AND UserAgent NOT LIKE '%Yahoo! Slurp%'
AND UserAgent NOT LIKE '%Exabot%'
AND UserAgent NOT LIKE '%Ezooms%'
AND UserAgent NOT LIKE '%facebookexternalhit%'
AND UserAgent NOT LIKE '%YPBot%'
AND UserAgent NOT LIKE '%Nutraspace%'
AND UserAgent NOT LIKE '%AhrefsBot%'
AND UserAgent NOT LIKE '%Gigabot%'
AND UserAgent NOT LIKE '%GT::WWW%'
AND UserAgent NOT LIKE '%WGet%'
AND UserAgent NOT LIKE '%cmsworldmap%'
AND UserAgent NOT LIKE '%CatchBot%'
AND UserAgent NOT LIKE '%SuperPages%'
AND UserAgent NOT LIKE '%msnbot%'
AND UserAgent NOT LIKE '%nutch%'
AND UserAgent NOT LIKE '%Yeti%'
AND UserAgent NOT LIKE '%Curl%'
AND UserAgent NOT LIKE '%Mediapartners-Google%'
AND UserAgent NOT LIKE '%Python-urllib%'
AND UserAgent NOT LIKE '%AC-BaiduBot%'
AND UserAgent NOT LIKE '%MLBot%'
AND UserAgent NOT LIKE '%YahooCacheSystem%'
AND UserAgent NOT LIKE '%Xenu%'
AND UserAgent NOT LIKE '%DoCoMo%'
AND UserAgent NOT LIKE 'Content Crawler'
GROUP BY Browser, Version
ORDER BY Count DESC

它不是太漂亮但它的工作到目前为止。我得到的结果如下:

Browser            Version      Count
Mozilla            5.0          804
IE                 8.0          738
AppleMAC-Safari    5.0          429
IE                 7.0          371
IE                 6.0          271
Firefox            6.0          189
IE                 9.0          137
Firefox            5.0          68
Firefox            3.6.20       42
Firefox            3.6.10       33
Firefox            4.0.1        20
Unknown            0.0          17
Firefox            3.6.18       10
Firefox            5.0.1        9
Mozilla            1.9.2.20     8
Firefox            3.6.13       6
Opera              9.80         6
Firefox            3.6.3        5
Firefox            3.6.12       4
Opera              9.24         4
IE                 5.5          4
Mozilla            1.9.2.16     3
Firefox            3.6.21       3
Firefox            3.6.6        3
Firefox            3.6.8        3
Firefox            4.0          3
Mozilla            4.0          3
Firefox            3.5.19       3

我需要对T-SQL做什么来组合具有相同主版本号和次版本号的版本,而不管其他版本号是什么?例如,我希望将这些全部组合在一起作为版本3.6

Firefox            3.6.20       42
Firefox            3.6.10       33
Firefox            3.6.18       10
Firefox            3.6.13       6
Firefox            3.6.3        5
Firefox            3.6.12       4
Firefox            3.6.21       3
Firefox            3.6.6        3
Firefox            3.6.8        3

1 个答案:

答案 0 :(得分:2)

非常讨厌,但你可以用这个表达式替换Version,以便在第二个小数点之前得到所有文本:

LEFT(Version, CHARINDEX('.', Version + '.', CHARINDEX('.', Version) + 1) - 1)

这假定每个版本至少有一个小数点(例如1.0)。