Groups | Blog | Home
all groups > sql server programming > december 2003 >

sql server programming : Column with runing values


Tom Moreau
12/30/2003 1:59:57 PM
Try:

select (select count (*) from authors b
where b.au_lname < a.au_lname
or (b.au_lname = a.au_lname
and (b.au_fname < a.au_fname
or (b.au_fname = a.au_fname
and b.au_id <= a.au_id)))) as mycolumn
, au_id,au_lname,au_fname
from authors a
order by au_lname, au_fname


--
Tom

---------------------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql


[quoted text, click to view]
Hi all,

Its great to see that so many ppl helpout each other. I hope I can do the
same on day :), anyway Can any one please guide me that how can i make a
runing column in result set e.g.

i have a query

select au_id,au_lname,au_fname from authors

which returns

au_id au_lname
au_fname
----------- -------------------------------- -------- -----------
---------
409-56-7008 Bennet Abraham
648-92-1872 Blotchet-Halls Reginald
238-95-7766 Carson Cheryl
722-51-5454 DeFrance Michel
712-45-1867 del Castillo Innes
427-17-2319 Dull Ann
213-46-8915 Green Marjorie


how can i add another column which shows

mycolumn au_id au_lname au_fname
----------- ----------- ---------------------------------------- -----------
---------
1 409-56-7008 Bennet Abraham
2 648-92-1872 Blotchet-Halls Reginald
3 238-95-7766 Carson Cheryl
4 722-51-5454 DeFrance Michel
5 712-45-1867 del Castillo Innes
6 427-17-2319 Dull Ann
7 213-46-8915 Green Marjorie

I would really apericate the help


Regards

Raymond D'Anjou (raydan)
12/30/2003 2:45:15 PM
Although Tom's solution works (and is verrrry interesting),
running this on a relatively small table in my database
takes about 10 times as long as one without the "count" column.
This is best done client side.
Very easy to use a counter and generate this.

[quoted text, click to view]

Tom Moreau
12/30/2003 2:53:49 PM
Hey, I'm the first to agree. It really is a front-end job to do, but hey
when you have a hammer - SQL - everything looks like a nail. ;-)

--
Tom

---------------------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql


[quoted text, click to view]
Although Tom's solution works (and is verrrry interesting),
running this on a relatively small table in my database
takes about 10 times as long as one without the "count" column.
This is best done client side.
Very easy to use a counter and generate this.

[quoted text, click to view]
Raymond D'Anjou (raydan)
12/30/2003 4:00:14 PM
LOL. More like a sqledgehammer.
[quoted text, click to view]
Hey, I'm the first to agree. It really is a front-end job to do, but =
hey when you have a hammer - SQL - everything looks like a nail. ;-)

--=20
Tom

---------------------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
Tom Moreau
12/30/2003 4:01:34 PM
Heh, heh, heh... :-)

--
Tom

---------------------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql


[quoted text, click to view]
Hey, I'm the first to agree. It really is a front-end job to do, but hey
when you have a hammer - SQL - everything looks like a nail. ;-)

--
Tom

---------------------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
Otis B. Driftwood
12/30/2003 4:43:37 PM
[quoted text, click to view]

I'm shocked,shocked! :~)

[quoted text, click to view]

This is why other enterprise databases have sql99 OLAP functions
to do this kind of thing.(You can even do this in MySql without
that hideous ancient subquery:).If Yukon doesn't have them you can still
stick to your point, otherwise prepare to eat some crow:~)

Happy New Year,
OBD





Delbert Glass
12/30/2003 5:32:07 PM
It's no surprise, that a

K_Large * O(n*n) process is slower than a
K_Small * O(n) process.

Meanwhile, K_Large can become K_Reduced via:
create index HowAboutMe on authors(au_lname, au_fname, au_id)

and furthere reduced to K_FurtherReduced via:
create unique clustered index HowAboutMe on authors(au_lname, au_fname,
au_id)

Bye,
Delbert Glass

[quoted text, click to view]

Naveed Akbar
12/30/2003 11:46:17 PM
Hi all,

Its great to see that so many ppl helpout each other. I hope I can do the
same on day :), anyway Can any one please guide me that how can i make a
runing column in result set e.g.

i have a query

select au_id,au_lname,au_fname from authors

which returns

au_id au_lname
au_fname
----------- -------------------------------- -------- -----------
---------
409-56-7008 Bennet Abraham
648-92-1872 Blotchet-Halls Reginald
238-95-7766 Carson Cheryl
722-51-5454 DeFrance Michel
712-45-1867 del Castillo Innes
427-17-2319 Dull Ann
213-46-8915 Green Marjorie


how can i add another column which shows

mycolumn au_id au_lname au_fname
----------- ----------- ---------------------------------------- -----------
---------
1 409-56-7008 Bennet Abraham
2 648-92-1872 Blotchet-Halls Reginald
3 238-95-7766 Carson Cheryl
4 722-51-5454 DeFrance Michel
5 712-45-1867 del Castillo Innes
6 427-17-2319 Dull Ann
7 213-46-8915 Green Marjorie

I would really apericate the help


Regards

Naveed Akbar

Raymond D'Anjou (raydan)
12/31/2003 9:28:51 AM
I don't know what world I'm living in but I have never felt the need to
'number' a recordset in SQL.
So I am sticking to my point, no matter what (at my age, I have the right to
do this).
I don't know MySQL but probably will meet Yukon someday (when is that,
2006???).

Besides that, sorry to have shocked you. :-(

I have never eaten crow but I would try it. Must taste like chicken. :-)

Happy New Year

[quoted text, click to view]

Joe Celko
12/31/2003 9:30:32 AM
[quoted text, click to view]

Likewise. The newbies don't know about tiered architectures or networks
and do not have multiple programming language skills because they grew
up with a PC or (shudder) a video game view of the world. They honestly
think that playing "mousey clicky" is programming -- look at the number
of posting here where the guy doesn't know whst DDL is and cannot write.

[quoted text, click to view]
that, 2006???). <<

MySQL is a file system with a near-SQL dialect on the front. It is
handy when your web app needs a file.

I am starting to think that waiting for Yukon is much like marrying a
woman who promises you how good its going to be .. next year.

[quoted text, click to view]
<<

Somewhere between Spotted Owl and Whopping Crane, but not as tough as
Bald Eagle.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Otis B. Driftwood
12/31/2003 9:58:19 AM

[quoted text, click to view]
Remember Casablanca:)

[quoted text, click to view]

Many times a little personality can go a lot further that a lot of good sql.
Maybe in your world too :~)

HTH :)



Raymond D'Anjou (raydan)
12/31/2003 11:51:12 AM
[quoted text, click to view]
I told you I was old. I think I was there for the premiere. :-(

[quoted text, click to view]
Of course.
I have enough to laugh at myself and try to make other people laugh (or at
least smile).
I do confess that my SQL skills are not up to par with some other posters
here.
I'm here to learn (yeah, even at my age) and to try to contribute.

To get back to the subject, I'm in the group that believes that a database
exists to organize, store and return data, not present it.
But I cheat and format every once and a while
(SELECT CONVERT(CHAR(10), GETDATE(), 111)) ;-)

Raymond D'Anjou (raydan)
12/31/2003 12:41:29 PM
[quoted text, click to view]

Heh, Joe agrees with me!!!
Pinch me. Ouch. Quick, what's the date?
I've got to write this down. :-))))

Joe Lax
1/1/2004 6:28:46 PM
Correct me if I'm wrong, but isn't this the type of subquery you have to
write for the "top three salesman" type problem? (First introduced to me
personally when I read one of Celko's books. <s> ). So, putting aside the
argument regarding whether or not to number a rowset in the front or the
back end, would it be a good idea if databases did have optimizations to
handle this concept?

Joe Lax


[quoted text, click to view]

AddThis Social Bookmark Button