I have a bad table structure. I try to modify it as little as possible because the real problem is more complicated. I'm working in SQL Server 2005.
Here is my table:
tbl_item
id | item
---+------
1 | car
2 | car
3 | car, => This is what I focus
4 | jet
5 | jet, car => This is just an example of the comma purpose
Query :
SELECT item, count(*) AS sum
FROM tbl_item
GROUP BY item
ORDER BY sum DESC
Result:
item | sum
--------+-------
car | 2
car, | 1
jet | 1
jet,car | 1
What I want is like this:
item | sum
---------+--------
car | 3
jet | 1
jet, car | 1 => actually I don't care about this, but this just for example
I tried :
SELECT REPLACE(item,',',''), count(*) AS sum
FROM tbl_item
GROUP BY item
ORDER BY sum DESC
But the result is:
item | sum
---------+------
car | 2
car | 1 => still in the different row
jet | 1
car, jet | 1
I can manipulate this so easy with PHP, but I wonder how to do this with pure SQL Server.