Hi,
I cant figure out how to do this....
for example:
Select name from mytab order by col1
could return
Mike
Dave
Sue
Simon
Paul
Fred
I would like to show the row number, like in the grid in query analyser. so
the orginal sort order is preserved
i.e
1 Mike
2 Dave
3 Sue
4 Simon
5 Paul
6 Fred
Any ideas? 9 1464
Mike,
Something along these lines will do it. If you are using SQL Svr 2000
use a table var instead of a temp table.
HTH
Pachydermitis
--create a temp table the will add the numbers for you
create table #whatever(NameID int not null identity(1,1), PersonsName
varchar(30))
--write the values to the table
INSERT INTO #whatever
Select name from mytab order by col1
--return the values
SELECT * FROM #whatever
--clean up
DROP TABLE #whatever
"Pachydermitis" <de******@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com... Mike, Something along these lines will do it. If you are using SQL Svr 2000 use a table var instead of a temp table. HTH Pachydermitis
--create a temp table the will add the numbers for you create table #whatever(NameID int not null identity(1,1), PersonsName varchar(30)) --write the values to the table INSERT INTO #whatever Select name from mytab order by col1 --return the values SELECT * FROM #whatever --clean up DROP TABLE #whatever
Thanks for this
Kind Regards
Mike
Since a table is **by definition** not sorted, what do you think the
"the orginal sort order" is?? Sequental files with their record
numbers behave like this, but that has nothing to do with an RDBMS.
The only way to guarantee an output in a particular order is to use an
ORDER BY clause which is part of a cursor. I would guess from this
post that you need to get a book on RDBMS foundations before you write
any code.
"Mike R" <ne**@mikeread.freeserve.co.uk> wrote in message
news:da*******************@news.demon.co.uk... Hi,
I cant figure out how to do this....
for example: Select name from mytab order by col1
could return
Mike Dave Sue Simon Paul Fred
I would like to show the row number, like in the grid in query analyser. so the orginal sort order is preserved
i.e 1 Mike 2 Dave 3 Sue 4 Simon 5 Paul 6 Fred
Any ideas? http://www.aspfaq.com/show.asp?id=2427
In general, you should do this on the client side, not the server side.
Simon
"--CELKO--" <jc*******@earthlink.net> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com... Since a table is **by definition** not sorted, what do you think the "the orginal sort order" is?? Sequental files with their record numbers behave like this, but that has nothing to do with an RDBMS.
The only way to guarantee an output in a particular order is to use an ORDER BY clause which is part of a cursor. I would guess from this post that you need to get a book on RDBMS foundations before you write any code.
I kept the posted example "very simple". I wanted to know how to get a row
number. My actual query has an order clause , it is passed to an
application that inserts another order clause which I cannot remove by any
method.
Thanks for the advice on the book, however, I dont need it.
"Simon Hayes" <sq*@hayes.ch> wrote in message
news:42**********@news.bluewin.ch... "Mike R" <ne**@mikeread.freeserve.co.uk> wrote in message news:da*******************@news.demon.co.uk... Hi,
I cant figure out how to do this....
for example: Select name from mytab order by col1
could return
Mike Dave Sue Simon Paul Fred
I would like to show the row number, like in the grid in query analyser. so the orginal sort order is preserved
i.e 1 Mike 2 Dave 3 Sue 4 Simon 5 Paul 6 Fred
Any ideas?
http://www.aspfaq.com/show.asp?id=2427
In general, you should do this on the client side, not the server side.
Simon
Thanks Simon - great links, In the end I wrote a cursor in my SQL function
to add the identifier.
>> Since a table is **by definition** not sorted, what do you think the
"the orginal sort order" is?? Sequental files with their record
numbers behave like this, but that has nothing to do with an RDBMS.
<<
OK, I've got a hotel with a dozen empty rooms, and a bunch of guests
arriving at once.
Using ROW_NUMBER(), available in DB2 and Oracle, I can very easily
assign a room for each guest - it would be a snap. Even though we know
that rows are not records etc, ROW_NUMBER() is a great and performant
alternative to cursors.
Having said all that, I can assign rooms to guests using pure SQL
without ROW_NUMBER, even if some guests have duplicate names. I can do
it either way no problem:
create table room(num int primary key, guest_name varchar(50))
go
insert into room(num)values(1)
insert into room(num)values(2)
insert into room(num)values(3)
insert into room(num)values(4)
insert into room(num)values(5)
go
create table guest(guest_name varchar(50))
go
insert into guest values('Brown, Jackie')
insert into guest values('Smith, Ann')
insert into guest values('Smith, Ann')
insert into guest values('Tower, Joe')
go
create table sequence_tab(n int)
go
insert into sequence_tab values(1)
insert into sequence_tab values(2)
insert into sequence_tab values(3)
insert into sequence_tab values(4)
insert into sequence_tab values(5)
go
update room set guest_name = guest_distinct.guest_name
from room,
(select guest_name, count(*) num
from guest group by guest_name) guest_distinct,
sequence_tab
where sequence_tab.n <= guest_distinct.num
and room.num = sequence_tab.n +
(select count(*) from guest
where guest.guest_name < guest_distinct.guest_name)
go
select * from room
go
drop table room
drop table guest
drop table sequence_tab
I just think the approach I've just demonstrated is a little bit too
complex, and the one with numbering rows easy to understand and
maintain:
using the same set up
create table #guest(id int identity(1,1), guest_name varchar(50))
go
insert into #guest(guest_name) select guest_name from guest
go
update room set guest_name = null
go
update room set guest_name = #guest.guest_name
from room, #guest
where room.num = #guest.id
I'd go for the second solution any time of the day, even though I have
come up with a pure SQL solution just as well. Simplicity in my opinion
is a great advantage. So, even though I know pretty well, for more than
a decade, that "rows are not records etc." I also know that there are
real life situations when numbering rows comes very handy.
What do you think?
AK (st***********@gmail.com) writes: OK, I've got a hotel with a dozen empty rooms, and a bunch of guests arriving at once. Using ROW_NUMBER(), available in DB2 and Oracle,
And not only that - it's in SQL 2005 as well. A great addition!
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp
Mike R (ne**@mikeread.freeserve.co.uk) writes: Thanks Simon - great links, In the end I wrote a cursor in my SQL function to add the identifier.
That is not likely to be good for performance. Using a temp table
or a table variable with an IDENTITY column as demonstrated in
other posts, is probably the best way to go.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: unixman |
last post by:
As usual, it is 2:00am, and I'm pulling my hair out, finally resorting to
posting in the newsgroups for help. :)
Simple problem, in theory.
Given table "map":
CREATE TABLE map (
entry_id...
|
by: Joseph Barron |
last post by:
Here is a SIMPLE problem that I'm trying to solve.
It works in Netscape 6.2, but IE6 gives ""No such interface
supported."
Below are page1.htm and page2.htm .
In page1.htm, there are two...
|
by: Gerry |
last post by:
I am relatively new to DB2 and having a problem with a simple union
statement.
Running Db2UDB version 8.1.1 on Aix 5.1
The union and union all SQL statements I am running produce the same...
|
by: John Baker |
last post by:
Hi:7
Newby here to ASP, and using the ASP.NET Web Matrix development tool. While that tool
looks great for a Newby, I have run into a snag. I have an HTML Text Box which I have
named HireInput,...
|
by: Tim::.. |
last post by:
Can someone tell me how I convert this simple SQL statement so I can use it
in ASP.NET???
I have an issue with the quotation marks and wondered if there is a simple
rule for converting the sql...
|
by: dba_222 |
last post by:
Dear Experts,
Ok, I hate to ask such a seemingly dumb question, but I've
already spent far too much time on this. More that I
would care to admit.
In Sql server, how do I simply change a...
|
by: Emin |
last post by:
Dear Experts,
I have a fairly simple query in which adding a where clause slows
things down by at least a factor of 100. The following is the slow
version of the query
...
|
by: Ted |
last post by:
In MS SQL I used the following to create a stored procedure.
USE AdventureWorks;
GO
IF OBJECT_ID ( 'HumanResources.usp_My_Search', 'P' ) IS NOT NULL
DROP PROCEDURE HumanResources.usp_My_Search;...
|
by: SuperFool |
last post by:
This has got to be one of those questions only a serious newbie would come up with....
Basicly: I select all the city names in the table and turn them into a pull down menu (code below)
print...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |