I have a table with the structure below:
create table roster
(
date date not null,
first nvarchar(20) not null,
second nvarchar(20) not null,
third nvarchar(20) not null,
fourth nvarchar(20) not null,
)
go
And that has the following data inserted:
insert into roster values ('2015-06-10 12:45:34', 'e', 'm', 'a', 'r')
insert into roster values ('2015-06-11 12:45:34', 'e', 'v', 'a', 'r')
insert into roster values ('2015-06-12 12:45:34', 'e', 'm', 'a', 'd')
insert into roster values ('2015-06-13 12:45:34', 'e', 'm', 'a', 'd') *
insert into roster values ('2015-06-14 12:45:34', 'e', 'm', 'a', 'r')
insert into roster values ('2015-06-15 12:45:34', 'e', 'm', 'a', 'r') *
insert into roster values ('2015-06-16 12:45:34', 'z', 'm', 't', 'r')
Note: * marks duplicates.
How do I select only one unique consecutive combination of "first", "second", "third", and "fourth"? For example, with the above inserted data, the desired output is:
Date First Second Third Fourth
2015-06-10 12:45:34, e m a r
2015-06-11 12:45:34, e v a r
2015-06-12 12:45:34, e m a d
2015-06-14 12:45:34, e m a r
2015-06-16 12:45:34, z m t r
I am looking for a solution that preserves entries when they are no longer consecutive (or when the sequence is broken), but removes duplicates of consecutive entries.
I have seen similar questions posted here, but I have been uable to make a solution that uses a group by.
Any help would be appreciated
Why need a
group by
if you just need a singleexists()
?SQL Fiddle
EDIT
SQL Fiddle