Dataphor SQL RAC (Relational Application Companion)


A site of hope for those looking for a true relational database system

Sunday, June 24, 2007

Sql - Using a dense rank for identifying sections

In his July 2007 MS Sql Server article Itzik Ben-Gan raises the question
of identifying consecutive runs of rows where values repeat themselves. 

T-SQL Black Belt 
Identifying Sections 
By:Itzik Ben-Gan 

The problem is framed as:

'The generic form of the problem involves a table (call it T1) that has
 two columns of interest: one representing order among rows (call it id)
 and the other holding some value (call it val). The task at hand is to
 identify sections of consecutive rows that share the same value. 
 The terms section and consecutive rows are problematic when you're 
 dealing with sets, but as I mentioned, the column ID represents logical
 order among rows, and once order is defined, these terms become meaningful.
 For each identified section, you need to return the minimum id, maximum id,
 val, count of rows in the section, and possibly other aggregates.'
 
The use of the term 'generic' is most appropriate as it conveys the idea
that a proposed solution account for 'all' data. Such a generic approach
is an 'inclusive' one.

Given the sample data:

create table TG1
(
 id  int not null primary key,
 val varchar(10) not null
);

insert into TG1(id, val) values( 1, 'a');
insert into TG1(id, val) values( 2, 'a');
insert into TG1(id, val) values( 3, 'a');
insert into TG1(id, val) values( 5, 'a');
insert into TG1(id, val) values( 7, 'b');
insert into TG1(id, val) values( 9, 'b');
insert into TG1(id, val) values(11, 'a');
insert into TG1(id, val) values(13, 'a');
insert into TG1(id, val) values(17, 'b');
insert into TG1(id, val) values(19, 'b');
insert into TG1(id, val) values(23, 'b');
insert into TG1(id, val) values(29, 'a');
insert into TG1(id, val) values(31, 'b');
insert into TG1(id, val) values(37, 'b');

The ideal solution is to create a new column which can be
used with val to group the rows into discrete sections which
reflects the fact that a particular val can occur repeated times.

id   val    Grp(Dense_Rank) 
---- ------ --------------- 
1    a      1
2    a      1
3    a      1
5    a      1
7    b      2
9    b      2
11   a      3
13   a      3
17   b      4
19   b      4
23   b      4
29   a      5
31   b      6
37   b      6

The new column (Grp) is simply a dense rank on val in the order
of id. 

There are two proposed approaches. One uses a subquery to create the
Grp column. Various type of subqueries have been proposed as I did
some time ago:

Jul 5 1999, 12:00 am
microsoft.public.sqlserver.programming
Difficult SQL Question
Author: Trysql

Jun 11 2001, 10:33 am
microsoft.public.sqlserver.programming
SQL challenge: coelesce consecutive and overlapping bookings for a room
Author: steve dassin

None of the subquery solutions are particularly appealing. They are not
intuitive and it is doubtful the average developer would come upon
such a beast. And they are certainly not efficient as described here.
As for the analytic ranking functions as explained here sql ranking
functions cannot rank a column independent of its order. This makes it
necessary to simulate a dense rank with a combination of functions.
This too is not intuitive and it is doubtful the average developer
understands this approach and can apply it to other similar problems.
We are really talking expert sql programmers and not application
developers.

Perhaps a more fundamental issue than the types of solutions is the
question posed. A generic solution is the foundation of a report.
But of how much importance is the relationship between different
vals. Is the intent to see a relationship between different accounts,
students,sport teams or to examine data/relationships within the
same team but different sections? If the latter then is it really
necessary to produce sections on everything?

If the question is framed as identify all the sections for a 'particular'
val, as seems reasonable, the question of the types of solutions
and there nature dramatically changes. Now the whole solution can be
based on the very basic idea of a dense rank - if the thing is the
same use the current rank, if another thing increment the rank. Any
developer using any client should have no problem seeing this as
nothing more than a foreach loop.
Assuming val 'a' is of interest in D4 this can be done as simply:

var TempTable:=table of{id:Integer,val:String,Grp:Integer}{};
var I:Integer:=1;
foreach var LItem in TG1 do
 begin
 if LItem.val<>'a' then
    I:=I+1;
 if LItem.val='a' then   
 insert row{LItem.id id,LItem.val val,I Grp} into TempTable;
 end;
 select TempTable;

id val Grp 
-- --- --- 
1  a   1   
2  a   1   
3  a   1   
5  a   1   
11 a   3   
13 a   3   
29 a   6   

We have completely eliminated any need of subqueries or ranking
functions. In fact we have eliminated any need of any type of query.

We can, of course, create a procedure to return sections for any
val. And number the sections consecutively in case that is helpful.

A D4 procedure would be:

create operator Sections_for_a_val(aTable:typeof(TG1),aVal:String):
                        table{id:Integer,val:String,Grp:Integer}
begin                        
result:=table of typeof(result){};
var I:Integer:=1;
foreach row in aTable do
 begin
 if val<>aVal then
    I:=I+1;
 if val=aVal then   
 insert row{id id,val val,I Grp} into result;
 end;     
result:=
       result
         join
            ( 
             ToTable(ToList(cursor(result over{Grp} order by {Grp})))
              {Grp,sequence+1 GrpSeq}
            )
             {id,val,GrpSeq Grp}; 
end;

select Sections_for_a_val(TG1,'a');

id val Grp 
-- --- --- 
1  a   1   
2  a   1   
3  a   1   
5  a   1   
11 a   2   
13 a   2   
29 a   3   

select Sections_for_a_val(TG1,'b');

id val Grp 
-- --- --- 
7  b   1   
9  b   1   
17 b   2   
19 b   2   
23 b   2   
31 b   3   
37 b   3   

Summarizing is the same as in the sql solutions.

select Sections_for_a_val(TG1,'a')
  group by {Grp}
    add{Min(id) start_section,Max(id) end_section,Count() num_rows};

Grp start_section end_section num_rows 
--- ------------- ----------- -------- 
1   1             5           4        
2   11            13          2        
3   29            29          1 

Finally, least I have neglected sql, here is yet another subquery
approach (in D4) to dense ranks that takes the sql server 2005
row_number() function as its starting point.

var A:=
   SQLQuery('select id,val,row_number()over(order by id) RowID
               from TG1');
select A
  add
      {
       Count(
             (A rename {RowID RowID1} where RowID1<=RowID)
              add{ (A adorn{key{RowID}})[RowID1-1].val
                    =
                    (A adorn{key{RowID}})[RowID1].val Testval}
                       where not Testval
             ) + 1          
           DenseRank
       };  
       
id val RowID DenseRank 
-- --- ----- --------- 
1  a   1     1         
2  a   2     1         
3  a   3     1         
5  a   4     1         
7  b   5     2         
9  b   6     2         
11 a   7     3         
13 a   8     3         
17 b   9     4         
19 b   10    4         
23 b   11    4         
29 a   12    5         
31 b   13    6         
37 b   14    6     

There is certainly nothing 'wrong' with a report. But it is perhaps
wrong-headed to try to make one when it is not really the object
of the exercise.

steve

277 comments:

«Oldest   ‹Older   201 – 277 of 277
Anonymous said...

Hi it's me, I am also visiting this website on a regular basis, this web site is actually good and the viewers are actually sharing nice thoughts.

Also visit my site cedar binary options

Anonymous said...

Unquestionably believe that that you stated. Your
favourite justification seemed to be on the web the simplest factor to
take note of. I say to you, I definitely get irked
while other folks consider worries that they plainly don't know about. You controlled to hit the nail upon the highest and also outlined out the entire thing without having side-effects , other folks can take a signal. Will probably be again to get more. Thank you

Check out my blog ... cedar finance binary trading

Anonymous said...

This design is steller! You obviously know how to keep a reader entertained.
Between your wit and your videos, I was almost moved to start my own blog (well,
almost...HaHa!) Wonderful job. I really enjoyed what you had to say, and more than that, how
you presented it. Too cool!

My webpage: forex factory calendar

Anonymous said...

Hello there! This post couldn't be written any better! Looking through this article reminds me of my previous roommate! He always kept preaching about this. I am going to forward this information to him. Fairly certain he will have a great read. I appreciate you for sharing!

my web blog :: binary options trading strategy
My site - Binary Options trading platform

Anonymous said...

Exceptional post but I was wanting to know if you could
write a litte more on this subject? I'd be very grateful if you could elaborate a little bit further. Kudos!

my web blog: binary options Strategies
My page: binary options trading system

Anonymous said...

At this time it seems like BlogEngine is the best blogging platform available right now.
(from what I've read) Is that what you are using on your blog?

Feel free to visit my web-site :: How To Make Money On Twitter

Anonymous said...

I am really loving the theme/design of your web site.
Do you ever run into any web browser compatibility problems?
A small number of my blog visitors have complained about my blog not
operating correctly in Explorer but looks
great in Opera. Do you have any recommendations to help
fix this issue?

My homepage: awesome penny stocks review

Anonymous said...

Hello there, just became aware of your blog through Google, and found that it is
really informative. I'm gonna watch out for brussels. I will be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers!

Also visit my web blog: comefaresoldi
Also see my website :: come riuscire a fare soldi

Anonymous said...

Wow, fantastic blog layout! How long have you been blogging for?
you made blogging look easy. The overall look
of your web site is magnificent, let alone the content!

My web page top ten ways to make extra money
Also see my web site: Ways To Make fast money

Anonymous said...

Magnificent beat ! I would like to apprentice at the same
time as you amend your site, how can i subscribe for
a blog website? The account aided me a applicable deal.
I had been tiny bit familiar of this your broadcast offered vibrant transparent concept

my web-site :: comefaresoldi
Also see my page: comefaresoldi

Anonymous said...

You actually make it seem so easy with your presentation but I in finding this
topic to be actually something which I think I might never understand.
It kind of feels too complex and very wide for me. I am taking a look
ahead in your next publish, I will try to get the hold of it!


Here is my web page gagnerdelargentrapidement
my website: gagnerdelargent

Anonymous said...

I pay a visit daily a few sites and information sites to read posts, but this website
gives quality based content.

Visit my web site: Binary Options Demo

Anonymous said...

I used to be suggested this website through my cousin.
I am no longer positive whether this put up is written by way of him as nobody else realize such special approximately
my difficulty. You are incredible! Thanks!

Look at my web blog paypal free money

Anonymous said...

Because the admin of this web page is working, no hesitation very soon
it will be renowned, due to its feature contents.



Have a look at my blog ... how to make money online free

Anonymous said...

Thanks for the good writeup. It in fact was once a enjoyment account it.

Look complex to far introduced agreeable from you!
By the way, how could we be in contact?

my webpage forex trading guide

Anonymous said...

Hello I am so delighted I found your weblog, I really found
you by error, while I was researching on Yahoo for something else,
Nonetheless I am here now and would just like to say thanks for a remarkable post and a all round thrilling blog (I also
love the theme/design), I don't have time to go through it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep up the excellent work.

Stop by my weblog :: cedar finance one touch
My website - hedging binary options

Anonymous said...

Great post. I was checking constantly this blog and
I am impressed! Extremely helpful information specifically
the last part :) I maintain such info a lot. I used to be seeking this certain information for a long time.
Thank you and good luck.

Also visit my blog :: Binäre Optionen

Anonymous said...

This website definitely has all of the information and
facts I needed about this subject and didn't know who to ask.

Look at my blog post ... online Easy jobs
my web site > online writing jobs for teenagers

Anonymous said...

Its such as you learn my thoughts! You seem to know so
much about this, like you wrote the e book in it or something.
I believe that you simply could do with a few p.
c. to pressure the message home a little bit, however other
than that, this is great blog. A great read.
I will definitely be back.

Here is my blog post :: im internet geld verdienen

Anonymous said...

Hi there, this weekend is pleasant for me,
because this time i am reading this fantastic educational article here at my residence.


Feel free to visit my homepage ... play online slots for money

Anonymous said...

I blog frequently and I truly appreciate your information.
This great article has really peaked my interest. I will book mark your website and keep
checking for new details about once a week. I opted in for
your Feed too.

Also visit my website; people looking for jobs

Anonymous said...

Thank you a lot for sharing this with all people you really realize
what you're talking approximately! Bookmarked. Kindly also seek advice from my web site =). We will have a hyperlink alternate arrangement between us

Feel free to surf to my web-site - jobs from home online

Anonymous said...

You're so cool! I do not believe I've truly read through anything like that before.
So wonderful to find someone with original thoughts on this
issue. Really.. many thanks for starting this up.
This website is something that is required on the web, someone with a little
originality!

Also visit my site: best buy work from home

Anonymous said...

Nice post. I learn something totally new and challenging on
sites I stumbleupon on a daily basis. It's always exciting to read content from other authors and practice something from their web sites.

Here is my web-site - earn money online from home

Anonymous said...

Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to more added agreeable from you!
By the way, how could we communicate?

Also visit my blog; best penny stocks

Anonymous said...

Heya this is kinda of off topic but I was wondering if blogs
use WYSIWYG editors or if you have to manually code with
HTML. I'm starting a blog soon but have no coding skills so I wanted to get advice from someone with experience. Any help would be enormously appreciated!

Look at my blog post :: easy way to earn money online

Anonymous said...

You could certainly see your skills within the work you write.
The arena hopes for more passionate writers such as you who are not afraid to say how they believe.
At all times go after your heart.

Also visit my page :: work from home no scams

Anonymous said...

Hmm is anyone else having problems with the pictures on this blog loading?
I'm trying to figure out if its a problem on my end or if it's the blog.
Any suggestions would be greatly appreciated.

Visit my weblog; make money fast and easy

Anonymous said...

It's really very difficult in this busy life to listen news on TV, therefore I simply use the web for that purpose, and obtain the hottest information.

My web blog :: making money at home online

Anonymous said...

Terrific work! This is the type of information that should
be shared around the web. Disgrace on Google for not positioning this put up upper!

Come on over and visit my site . Thank you =)

my homepage; work from home careers

Anonymous said...

Magnificent beat ! I would like to apprentice while you amend your web site,
how could i subscribe for a blog site? The account helped
me a acceptable deal. I had been a little bit acquainted of this
your broadcast offered bright clear concept

My blog ... online jobs real

Anonymous said...

Heya i'm for the first time here. I found this board and I find It truly useful & it helped me out a lot. I hope to give something back and help others like you aided me.

Look into my web page; make real money online

Anonymous said...

Hi, I do think this is an excellent web site. I stumbledupon it ;) I will return once again since I book-marked it.
Money and freedom is the best way to change, may you be rich and continue
to help other people.

Here is my web-site; online trading account

Anonymous said...

I have been surfing online more than three hours as of late, but I never discovered any
interesting article like yours. It's pretty worth sufficient for me. In my opinion, if all web owners and bloggers made good content material as you did, the net will likely be a lot more helpful than ever before.

Feel free to surf to my web-site forex trading forum

Anonymous said...

Good info. Lucky me I came across your blog by chance
(stumbleupon). I've book marked it for later!

Review my weblog: trading foreign currency online

Anonymous said...

Pretty! This was an extremely wonderful post. Thank you for
supplying this information.

Feel free to visit my web site how to trade gold

Anonymous said...

I don't even know how I ended up here, however I believed this put up was once good. I do not recognize who you're but
definitely you're going to a well-known blogger in case you aren't already.
Cheers!

Here is my web-site :: stock market trading software

Anonymous said...

Usually I do not learn article on blogs, however I would like to say that this write-up very compelled me to try and do so!
Your writing taste has been surprised me. Thanks, very great post.


my web blog :: best online money making sites

Anonymous said...

Excellent way of telling, and good article to take
data concerning my presentation subject, which i am going to present in institution
of higher education.

Feel free to surf to my weblog :: legit online jobs scam

Anonymous said...

Thanks for finally writing about > "Sql - Using a dense rank for identifying sections" < Loved it!

my page how can i make extra money online

Anonymous said...

Way cool! Some extremely valid points! I appreciate
you writing this write-up plus the rest of the site is really good.


Feel free to visit my website; binary option signals

Anonymous said...

Excellent beat ! I would like to apprentice while you amend your web site, how could i
subscribe for a weblog web site? The account aided me a applicable deal.
I have been a little bit acquainted of this your broadcast provided shiny clear concept

Here is my web-site :: jobs to apply online

Anonymous said...

Can you tell us more about this? I'd like to find out more details.

Here is my blog: forex pros

Anonymous said...

Hey There. I found your blog using msn. This is an extremely well written
article. I will be sure to bookmark it and come back to read more of your useful info.
Thanks for the post. I'll definitely return.

my webpage ... top ten highest paying jobs

Anonymous said...

wonderful issues altogether, you just received a brand new reader.
What could you recommend about your post that you made
a few days ago? Any sure?

Also visit my webpage :: what are binary options

Anonymous said...

I'm curious to find out what blog system you happen to be utilizing? I'm having some minor security problems with my latest site and I'd like to find something more risk-free. Do you have any suggestions?

Here is my page cedar finance

Anonymous said...

No matter if some one searches for his vital thing, so he/she
desires to be available that in detail, so that thing is maintained over here.


Feel free to visit my page ... http://www.youtube.com/watch?v=VMJkb-bhjiI

Anonymous said...

I am truly delighted to read this website posts which consists
of lots of helpful information, thanks for providing such statistics.


Feel free to surf to my page Commodities Options

Anonymous said...

Thanks for sharing your thoughts on how to trade forex.
Regards

Feel free to surf to my weblog: forex course

Anonymous said...

Fastidious answer back in return of this query with genuine arguments and explaining
all on the topic of that.

My webpage ... http://www.youtube.com/watch?v=MN36K29q7_Q

Anonymous said...

Its like you read my mind! You appear to grasp a lot approximately this, like you
wrote the ebook in it or something. I believe that you simply could do with a few % to power the message house a bit, however other than that, this is wonderful blog. An excellent read. I'll definitely be back.

Feel free to visit my webpage: Apply Jobs Online

Anonymous said...

Hi, I do think this is an excellent web site.
I stumbledupon it ;) I may return yet again since i have bookmarked it.
Money and freedom is the best way to change, may you be rich and continue to guide other people.


Feel free to surf to my blog post :: extra ways to make money

Anonymous said...

Hello there, I found your website by way of Google whilst searching for
a similar subject, your web site came up, it seems to be great.
I have bookmarked it in my google bookmarks.
Hi there, simply changed into alert to your weblog thru Google, and found
that it's really informative. I am gonna be careful for brussels. I'll be grateful if you
happen to proceed this in future. Numerous other folks might be
benefited from your writing. Cheers!

Here is my web page :: online money Making Jobs

Anonymous said...

Hi, i think that i saw you visited my site so i came to “return
the favor”.I am trying to find things to enhance my web site!

I suppose its ok to use some of your ideas!!

my page :: how to make money easy and quick

Anonymous said...

I used to be able to find good info from your blog posts.


Feel free to visit my website ... how To make money online with No money

Anonymous said...

Wow, awesome weblog format! How long have you ever been blogging for?
you make blogging glance easy. The full look of your website is fantastic,
as neatly as the content!

my web-site forex swing trading

Anonymous said...

Wow, amazing blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your web site is fantastic, as well as the content!


Here is my blog: how to make money online fast and free

Anonymous said...

Touche. Sound arguments. Keep up the good work.

Here is my web blog http://www.youtube.com/watch?v=FZOuC8FraM0

Anonymous said...

I am really impressed with your writing skills as well as
with the layout on your weblog. Is this a paid theme or did you customize it yourself?
Either way keep up the nice quality writing, it is rare to see a nice blog
like this one today.

Here is my web page: http://www.youtube.com/watch?v=ddcKJDTQ7zI

Anonymous said...

Very soon this web page will be famous amid all blog
visitors, due to it's fastidious content

Feel free to visit my blog post ... make money fast online

Anonymous said...

I simply could not depart your web site before suggesting
that I extremely loved the standard information an individual provide to your visitors?
Is gonna be again incessantly in order to investigate cross-check new posts

my web site how to make fast money online

Anonymous said...

I loved as much as you will receive carried out right here.

The sketch is tasteful, your authored subject matter stylish.
nonetheless, you command get bought an edginess over that you wish be
delivering the following. unwell unquestionably come further formerly again as exactly the same
nearly very often inside case you shield this increase.


my web blog - sub penny stocks

Anonymous said...

This design is steller! You definitely know how to
keep a reader amused. Between your wit and your videos, I was almost moved
to start my own blog (well, almost...HaHa!) Wonderful job.
I really enjoyed what you had to say, and more than
that, how you presented it. Too cool!

My homepage - cedar finance pay daily

Anonymous said...

Hi there friends, how is everything, and what you wish for
to say regarding this article, in my view its actually awesome for me.



my web blog: forex binary options

Anonymous said...

Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you! However, how could we
communicate?

My blog ... www.cedarfinance.com

Anonymous said...

Wonderful article! This is the kind of information that should be shared around the internet.
Shame on the search engines for no longer positioning this submit upper!
Come on over and talk over with my web site . Thanks =)

Visit my blog: easy ways to make money online for free

Anonymous said...

If some one desires to be updated with latest
technologies after that he must be pay a visit this web
page and be up to date everyday.

My blog; binary Options strategy

Anonymous said...

Link exchange is nothing else but it is just placing the other person's webpage link on your page at proper place and other person will also do same for you.

My web-site ways to make money fast

Anonymous said...

Remarkable things here. I'm very glad to see your post. Thanks so much and I'm
having a look forward to contact you. Will you kindly drop me
a e-mail?

Also visit my web-site ways to make money online from home

Anonymous said...

Hi, I do believe this is a great website. I stumbledupon it ;) I will
return once again since I bookmarked it. Money and freedom is the best way to change,
may you be rich and continue to help other people.


my website - http://www.youtube.com/watch?v=pwex99npRdc

Anonymous said...

This post is invaluable. How can I find out more?

Also visit my blog :: http://www.youtube.com/watch?v=Al2XPf2DgwE

Anonymous said...

Thanks for a marvelous posting! I quite enjoyed reading it, you will be a great author.
I will always bookmark your blog and definitely will come back later on.
I want to encourage continue your great job, have a nice morning!


Also visit my blog post ... best ways to make money online

Anonymous said...

I'm not sure exactly why but this weblog is loading very slow for me. Is anyone else having this problem or is it a problem on my end? I'll
check back later and see if the problem still exists.


Visit my web blog http://www.youtube.com/watch?v=jLuC1nG5cPk

Anonymous said...

Hello! I know this is kind of off-topic but I needed to ask.
Does managing a well-established blog such as yours require a large amount of work?
I'm brand new to running a blog however I do write in my diary daily. I'd like to start a blog so I can share my own experience and
feelings online. Please let me know if you have
any suggestions or tips for brand new aspiring blog
owners. Appreciate it!

Take a look at my homepage :: binary options trading strategy

Anonymous said...

Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn't show up. Grrrr... well I'm not writing all that over again.
Anyways, just wanted to say excellent blog!

Here is my web page http://www.youtube.com/watch?v=JoRWJriSgKc

Anonymous said...

Thank you for the good writeup. It in fact was a amusement account it.

Look advanced to far added agreeable from you! By the way, how
could we communicate?

my blog post: cedar finance binary option

Anonymous said...

I am regular reader, how are you everybody? This article posted
at this site is genuinely nice.

Also visit my site binary options systems

«Oldest ‹Older   201 – 277 of 277   Newer› Newest»

About Me

My photo
Phoenix, Arizona, United States