no need for a temp table, you can do it in one run.
see this example :
DECLARE
@tb TABLE (data1 VARCHAR(500) )
INSERT INTO @tb VALUES
('Delta,Charlie'),
('Delta,Bravo'),
('Bravo,Charlie'),
('Tango,Bravo'),
('Alpha')
SELECT data1, COUNT(*) total
FROM (
SELECT LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) data1
FROM (
SELECT CAST('<XMLRoot><RowData>' + REPLACE(data1,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) split
FROM @tb
) D
CROSS APPLY split.nodes('/XMLRoot/RowData')m(n)
) D
GROUP BY data1
More Explanation (per scsimon requested)
The method above is one of approaches that splits strings using XML. Basically what we did is replacing the commas from the actual data with XML tags </RowData><RowData>
that defines each split, then concat that with another XML tags <XMLRoot><RowData></RowData></XMLRoot>
that define each row.
So, if we get the first row for example Delta,Charlie
it'll be converted into :
<XMLRoot>
<RowData>Delta</RowData>
<RowData>Charlie</RowData>
</XMLRoot>
if you know how XML structure works or at least HTML (both have their similarities) , then you'll get the idea.
Then, we used XML techniques that SQL Server already supports, and selected the nodes (rows) , which then converted back to VARCHAR
to be treated as single value (regular row).
This is a quick explanation, while there are a lot of other methods some of them also uses XML, it depends on your preferences.
if you need other techniques, you can check this out , which covers almost all other techniques out there. (A good article too).
If you're using SQL Server 2016 or above, you can use STRING_SPLIT function, which will give you the same results.