How can I add row numbers to a result of a query:
SELECT ProjID, Descr FROM projects
WHERE EmpID = 123
need results in format:
1 123 First project
2 456 Second project
3 ...
I cannot find a simple solution.
Thanks
Because this function creates a column in a table, a name for the column
must be specified in the select list in one of these ways:
--(1)
SELECT IDENTITY(int, 1,1) AS ID_Num
INTO NewTable
FROM OldTable
--(2)
SELECT ID_Num = IDENTITY(int, 1, 1)
INTO NewTable
FROM OldTable
--
Keith
==============
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.
"alx" <g...@waymark.net> wrote in message
news:QYEd6.582$v77.1...@dfiatx1-snr1.gtei.net...
alx wrote in message ...
SELECT id = identity(int,1,1), ProjID, Descr
INTO #projects
FROM projects
WHERE EmpID = 123
SELECT *
FROM #projects
--
Thanks,
David Satz
Principal Software Engineer
Hyperion Solutions
->Using SQL Server 7.0 SP2/6.5 SP5a/Cold Fusion 4.5.1/ADO 2.1/VB 6.0/MTS
(Please reply to group only)
-----------------------------------------------------------------
"alx" <g...@waymark.net> wrote in message
news:QYEd6.582$v77.1...@dfiatx1-snr1.gtei.net...
But is there any way to just count rows in a query result without creating a
new table?
Alex
"Keith Kratochvil" <SendT...@novusprintmedia.com> wrote in message
news:#rUg8dviAHA.916@tkmsftngp04...
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Would you work from specifications like this for money?
There is no such thing as a "row number" in SQL. SQL is a set oriented
language and sets have no ordering!! This is the most basic concept of
the relational model.
The second most basic concept is that all relationships (like an
ordering) are shown as values in the columns of tables. The question
then becomes, "How can I get a sequential number in a column, based on
sorting my result set on column x?" I will assume that project_id is a
key.
SELECT (SELECT COUNT(*)
FROM Projects AS P1
WHERE P1.project_id <= P1.project_id) AS row_nbr
P2.project_id, P2.description
FROM Projects AS P2
WHERE emp_id = 123
ORDER BY project_id;
Get a good book on relational databases -- they are not like sequential
file systems, which is what I suspect your mental is.
--CELKO--
Joe Celko, SQL Guru & DBA at Trilogy
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc)
which can be cut and pasted into Query Analyzer is appreciated.
---
Trilogy, FORTUNE, and Goldman Sachs are hosting the E-Business event of
the year. Find out more by visiting www.battleroyale2001.com
Sent via Deja.com
http://www.deja.com/
Sounds like you want the .RowCount (or whatever it is called)
I am not sure how you are retrieving your data (select or stored procedure)
and I do not know where this data is going (asp recordset / component /
client), but does this work?
SELECT * FROM SomeTable
SELECT @@rowcount
Run it from Query Analyzer on some of your tables....
It might be what you are looking for.
--
Keith
==============
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.
"alx" <g...@waymark.net> wrote in message
news:VsFd6.641$v77.1...@dfiatx1-snr1.gtei.net...
One problem though:
If you try to order by au_lname
in your example, then your rownumber
will be out of order : (
Any other suggestions??
Alex
"a29379" <a29...@my-deja.com> wrote in message
news:#2Ia1eviAHA.2088@tkmsftngp04...
"Joe Celko" <71062...@compuserve.com> wrote in message
news:9578r2$nmp$1...@nnrp1.deja.com...
He accidently scratched a friend of mine now the poor
sob is gonna live forever.
Steve
"alx" <g...@waymark.net> wrote in message
news:aGGd6.680$v77.2...@dfiatx1-snr1.gtei.net...