Tuesday, 17 September 2013

SQL Count distinct values within the field

SQL Count distinct values within the field

I have this weird scenario (at least it is for me) where I have a table
(actually a result set, but I want to make it simpler) that looks like the
following:
ID | Actions
------------------
1 | 10,12,15
2 | 11,12,13
3 | 15
4 | 15,16,17
And I want to count the different actions in the all the table. In this
case, I want the result to be 8 (just counting 10, 11, ...., 17; and
ignoring the repeated values).
In case it matters, I am using MS SQL 2008.
If it makes it any easier, the Actions were previously on XML that looks like
<root>
<actions>10,12,15</actions>
</root>
I doubt it makes it easier, but somebody might comeback with an xml
function that I am not aware and just makes everything easier.
Let me know if there's something else I should say.

No comments:

Post a Comment