I am using MS SQL 2005
I have a problem, which currently I am battling to get a solution.
I have a Table, with a these columns : NameList;Time
The Namelist Column has comma delimited data in it. The table data is as such:
Namelist Time
John Smith, Jeremy Boyle, Robert Brits, George Aldrich 5
John Smith, Peter Hanson 15
Jeremy Boyle, Robert Brits 10
....
I need some sort of SQL expression that will provide me with this end result:
Name Total_Time
John Smith 20
Jeremy Boyle 15
Robert Brits 15
Etc……
Basically the expression must find All the names in the rows and math those names with the names in the other rows and add the times together for each user.
The Idea I have is to convert the comma delimited data into rows and count the distinct records of each then somehow know what the time for it is… then multiply….. but I have no idea on how to implement it
Any Help would be much appreciated
Thanks,
I prefer the number table approach to split a string in TSQL
For this method to work, you need to do this one time table setup:
Once the Numbers table is set up, create this split function:
You can now easily split a CSV string into a table and join on it:
OUTPUT:
Your can now use a CROSS APPLY to split every row in your table like:
OUTPUT:
Using this, I would recommend that you alter your table design and use this output to INSERT into a new table. That would be a more normalized approach. Also Don’t use reserved words for column names, it makes it a hassle. Notice how I use “NameOf” and “TimeOf”, so I avoid using reserved words.