如何对部分字段进行 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
Hello I have table in my database holding some browser information I'm currently using the following T-SQL to get a summary list:
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
Its not overly pretty but its working so far. I'm getting results that look like:
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
What do I need to do to my T-SQL to combine versions with the same major and minor version number, regardless of what the other build numbers are? For example I would like these to all be grouped together as Version 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
相当令人讨厌,但您可以用此表达式替换
Version
来获取第二个小数点之前的所有文本:这假设每个版本至少有一个小数点(例如 1.0)。
Pretty nasty, but you can replace
Version
with this expression to get all text up prior to the second decimal point:This assumes each version has at least one decimal point (e.g. 1.0).