views:

30

answers:

2

Given I have data like the following, how can I select and group by portions of a string?

Version  Users
1.1.1    1
1.1.23   3
1.1.45   1
2.1.24   3
2.1.12   1
2.1.45   3
3.1.10   1
3.1.23   3

What I want is to sum up the users using version 1.1.x and 2.2.x and 3.3.x etc, but I'm not sure how I can group on a partial string in a select statement.

edit What the data should return like is this:

Version  Users
1.1.XX   5
2.1.XX   7
3.1.XX   4

There is an infinite variable number of versions, some are in this format (major, minor, build) some are just major, minor and some are just major, the only time I want to "roll up" the versions is when there is a build.

A: 

You did not specify what you want to see after you group by the first portion nor any specification on the length of each subsection. Supposing that you want the count of users and that each section is no more than a single character (i.e., there is no 10.1.xxx nor 10.10.xxx):

Select substring(Version, 1, 3), Count(*)
From Table
Group By substring(Version, 1, 3)
Thomas
+1  A: 
select rtrim(Version, '0123456789') ||'XX', sum(users) 
from Table
group by rtrim(Version, '0123456789')
msi77
this is perfect. thank you
Russ Bradberry