Click to See Complete Forum and Search --> : Pretty tricky SQL select


Daniel Techen
June 10th, 2005, 11:51 AM
Hi folks,

today I spent a full hour trying to write a "simple" SQL SELECT:sick: . There is a table that has a column "position". The table looks like this:

SELECT RowNum, Position FROM myTable

RowNum Position
--------------------------
1 2a
2 2a
3 2a
4 2b
5 3a
6 3a
...


What I need is a SQL Select that does not retrieve a consecutive rownum, but a "rownum per position". The result table should look like this:

Position "RowNum"
--------------------------
2a 1
2a 2
2a 3
2b 1
3a 1
3a 2

I hope you got my point ;-). Can anybody give me a hint how to achieve this?

TIA,
Daniel

BTW. The DBMS is an Oracle.

chrisp
June 10th, 2005, 03:06 PM
GROUP BY Position

-or-

ORDER BY Position

should do the trick ... I think that's what you are looking for.

Chris

hspc
June 11th, 2005, 05:25 AM
From the resultset that Daniel sent, It does not require grouping
This query should work:
SELECT Position,RowNum FROM myTable
order by Position

JeffB
June 11th, 2005, 05:23 PM
Just to add a little thing, I would add a second order by: ;)

SELECT Position,RowNum FROM myTable
order by Position,RowNum

JeffB

cjard
June 21st, 2005, 06:01 PM
no, i think he actually wants a running total...

see his positions go up to 6, but he wants a incrementing counter for each distinct position

i can only offer this for calculating new records:


insert into table1
select count(*)+1, '2a' from table1 where pos = '2a'


count(*)+1 where pos = '2a' returns tha counter of all 2a, plus one. if 2a exists 10 times, this expression returns 11
the other literal text in there is '2a', which will literally be inserted..
this is oracle syntax

other rdbms may use select into

cjard
June 21st, 2005, 06:02 PM
ps; i thought about doing it using a running total style query (triangluar cartesian join) but couldnt get anything out of it..