如何对部分字段进行 GROUP BY?

发布于 2024-12-03 14:02:36 字数 2712 浏览 0 评论 0原文

您好,我的数据库中有一个表,其中保存了一些浏览器信息,我当前正在使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿童谣 2024-12-10 14:02:36

相当令人讨厌,但您可以用此表达式替换 Version 来获取第二个小数点之前的所有文本:

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

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

Pretty nasty, but you can replace Version with this expression to get all text up prior to the second decimal point:

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

This assumes each version has at least one decimal point (e.g. 1.0).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文