Home » SQL & PL/SQL » SQL & PL/SQL » Rownum per day (Oracle DB 11.2.0.4)
Rownum per day [message #643514] Fri, 09 October 2015 06:53 Go to next message
DirkHamburg
Messages: 5
Registered: May 2009
Location: Hamburg
Junior Member
Hello!

I have a table with three columns as follows:

NAME  CITY     DATE
==================================
Paul  Hamburg  01.10.2015 08:00
Jean  Berlin   01.10.2015 12:00
Anne  Munich   02.10.2015 08:00
Peter Cologne  02.10.2015 10:00
Mike  Hamburg  02.10.2015 12:00
John  Berlin   03.10.2015 06:00


I want to select this data and add a column, which displays the "Rownum per Day": Kind of ascending sequence per day.
Though the Rownum should be ascending depending on the datetime, the time can, but need not to be displayed in the resultset.

So, the result should look like this:

NAME  CITY     DATE              ROW_NUM
========================================
Paul  Hamburg  01.10.2015        1
Jean  Berlin   01.10.2015        2 
Anne  Munich   02.10.2015        1        [ <-- New Day: Restart at 1]
Peter Cologne  02.10.2015        2
Mike  Hamburg  02.10.2015        3
John  Berlin   03.10.2015        1        [ <-- New Day: Restart at 1]


I tried to do this with count() and "group by", but as "NAME" and "CITY" are not identical per day, this wouldn't work.

Has anybody a clue how to get this result?

Thanks a lot!

Dirk


Re: Rownum per day [message #643516 is a reply to message #643514] Fri, 09 October 2015 07:06 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Here's one option:
  SELECT name,
         city,
         datum,
         ROW_NUMBER () OVER (PARTITION BY TRUNC (datum) ORDER BY datum) rn
    FROM your_table
ORDER BY datum;
Re: Rownum per day [message #643517 is a reply to message #643516] Fri, 09 October 2015 07:13 Go to previous message
DirkHamburg
Messages: 5
Registered: May 2009
Location: Hamburg
Junior Member
Hey Littlefoot,

thanks a lot, you helped me very much!


Best regards,
Dirk
Previous Topic: set target table flag as D if rows deleted at source
Next Topic: what is the main usage of execute immediate.
Goto Forum:
  


Current Time: Thu Apr 25 07:15:32 CDT 2024