Paul Maddox

Software development team leader specialising in Microsoft Visual C# and C++ from the Northwest of England. Experience working in a globalised business and team; understanding of enterprise business operation and practices; experience reporting to executive management Skills in numerous languages and technologies; knowledge of formal software development lifecycle; experience of architecture design

Tuesday, April 21, 2009

SQL Quiz of the Day



Let’s say you have raw data as so:

Id Counter Ident
----------- ----------- -----
1 1 A
2 2 A
3 1 A
4 3 B
5 3 B
6 2 A
7 1 A
8 4 C
9 2 C
10 1 B
11 1 B
12 4 B

And you want to group by the Ident field, but
only contiguous blocks. So you want to display
the following:

Counter Ident
----------- -----
4 A
6 B
3 A
6 C
6 B

Naturally one can’t use GROUP BY, because this would
aggregate multiple A’s and B’s.

Answers on a postcard!

====

CREATE TABLE [dbo].[Test](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Counter] [int] NOT NULL,
[Ident] [varchar](3)
COLLATE Latin1_General_CI_AS NOT NULL,
CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

1 Comments:

Blogger Mark said...

My solution: http://snippets.dzone.com/posts/show/7232

April 21, 2009 8:42 AM  

Post a Comment

<< Home