Q:

Group by in SQL Server and then rows merge

I am using the below query to Count some data
select Count(BrowserName) as Count,BrowserName from UserSession
group by BrowserName

The result I get is as below
Count BrowserName
3      IE
4      InternetExplorer
5      Chrome
6      Safari

I was just wondering how can I group the IE and InternetExplorer together so I get results in the below format
Count BrowserName
7      IE
5      Chrome
6      Safari

Thanks

A:

You can use case:
select (case when BrowserName in ('IE', 'InternetExplorer') then 'IE'
             else BrowserName
        end) as BrowswerName,
       Count(*) as Count
from UserSession
group by (case when BrowserName in ('IE', 'InternetExplorer') then 'IE'
               else BrowserName
          end);

If you have a lot of matches, then using JOIN might be simpler:
select coalesce(v.new_browsername, us.browsername) as browsername,
       count(*)
from UserSession us left join
     (values ('InternetExplorer', 'IE')
     ) v(browsername, new_browsername)
     on us.browsername = v.browsername
group by coalesce(v.new_browsername, us.browsername);

You can add matched pairs in the values() list for further substitutions.

