try the following:
select ID, col1A=(case when col1='A' then 1 else 0 end), col1B=(case when col1='B' then 1 else 0 end) from <table>
IF you have one identifier with A and B, and you want to have a separate identifier in the output, which you could do
select ID, col1A=max(case when col1='A' then 1 else 0 end), col1B=max(case when col1='B' then 1 else 0 end) from <table> group by id
EDIT
According to your comment, if you do not know the number of options for col1, you can go to dynamic PIVOT
DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) select @cols = STUFF((SELECT distinct ',' + QUOTENAME(col1) from <table> FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set @query = 'SELECT id, ' + @cols + ' from <table> pivot ( count([col1]) for col1 in (' + @cols + ') ) p ' print(@query) execute(@query)
source share