I have the following table and data in SQL Server 2005:
create table LogEntries ( ID int identity, LogEntry varchar(100) ) insert into LogEntries values ('beans') insert into LogEntries values ('beans') insert into LogEntries values ('beans') insert into LogEntries values ('cabbage') insert into LogEntries values ('cabbage') insert into LogEntries values ('beans') insert into LogEntries values ('beans')
I would like to group repeated LogEntries so that I have the following results:
LogEntry EntryCount beans 3 cabbage 2 beans 2
Can you think of any way to do this in TSQL outside of using a cursor?
This is a set-based solution for the problem. The performance will probably suck, but it works 🙂
Results:
The ISNULL() is required for the first set of beans.