Maven4Champ
Technical User
- Jun 16, 2004
- 154
Hi everyone,
I have a query that has been bothering me the past 24 hours and I can't seem to get the result I need. Running Oracle 10g database and attempting to use row_number but it returns undesired results.
My data looks as follows:
CONTRACT YEAR PERIOD YEAR_NUM
CONTRACT-81352 2008 Sep-2008 1
CONTRACT-81352 2008 Oct-2008 2
CONTRACT-81352 2008 Nov-2008 3
CONTRACT-81352 2008 Dec-2008 4
CONTRACT-81352 2009 Jan-2009 5
CONTRACT-81352 2009 Feb-2009 6
CONTRACT-81352 2009 Mar-2009 7
CONTRACT-81352 2009 Apr-2009 8
CONTRACT-81352 2009 May-2009 9
CONTRACT-81352 2009 Jun-2009 10
CONTRACT-81352 2009 Jul-2009 11
CONTRACT-81352 2009 Aug-2009 12
CONTRACT-81352 2009 Sep-2009 13
CONTRACT-81352 2009 Oct-2009 14
CONTRACT-81352 2009 Nov-2009 15
CONTRACT-81352 2009 Dec-2009 16
CONTRACT-81352 2010 Jan-2010 17
CONTRACT-81352 2010 Feb-2010 18
CONTRACT-81352 2010 Mar-2010 19
CONTRACT-81352 2010 Apr-2010 20
CONTRACT-81352 2010 May-2010 21
CONTRACT-81352 2010 Jun-2010 22
CONTRACT-81352 2010 Jul-2010 23
CONTRACT-81352 2010 Aug-2010 24
CONTRACT-81352 2010 Sep-2010 25
CONTRACT-81352 2010 Oct-2010 26
CONTRACT-81352 2010 Nov-2010 27
CONTRACT-81352 2010 Dec-2010 28
CONTRACT-81352 2011 Jan-2011 29
CONTRACT-81352 2011 Feb-2011 30
CONTRACT-81352 2011 Mar-2011 31
CONTRACT-81352 2011 Apr-2011 32
CONTRACT-81352 2011 May-2011 33
CONTRACT-81352 2011 Jun-2011 34
CONTRACT-81352 2011 Jul-2011 35
CONTRACT-81352 2011 Aug-2011 36
CONTRACT-81352 2011 Sep-2011 37
My desired result is to have a 1 in the YEAR_NUM column for 2008, 2 in the YEAR_NUM column for 2009, 3 in the YEAR_NUM column for 2010 and 4 in the YEAR_NUM column for 2011.
Obviously, it is doing a row_number() based off the period as well but when removing the period and keeping my groupins within SQL, I get the desired results (kind of). Essentialy, removing the period fixes the problem. I then have 4 lines and 1, 2, 3, and 4 sequentially in the YEAR_NUM column. However, I must display the period as well, but I don't want that to affect/impact by row_number partition by statement. So ultimately for the first four rows, it should all say 1 in the YEAR_NUM.
Any SQL advice is greatly appreciated.
Thanks!
I have a query that has been bothering me the past 24 hours and I can't seem to get the result I need. Running Oracle 10g database and attempting to use row_number but it returns undesired results.
My data looks as follows:
CONTRACT YEAR PERIOD YEAR_NUM
CONTRACT-81352 2008 Sep-2008 1
CONTRACT-81352 2008 Oct-2008 2
CONTRACT-81352 2008 Nov-2008 3
CONTRACT-81352 2008 Dec-2008 4
CONTRACT-81352 2009 Jan-2009 5
CONTRACT-81352 2009 Feb-2009 6
CONTRACT-81352 2009 Mar-2009 7
CONTRACT-81352 2009 Apr-2009 8
CONTRACT-81352 2009 May-2009 9
CONTRACT-81352 2009 Jun-2009 10
CONTRACT-81352 2009 Jul-2009 11
CONTRACT-81352 2009 Aug-2009 12
CONTRACT-81352 2009 Sep-2009 13
CONTRACT-81352 2009 Oct-2009 14
CONTRACT-81352 2009 Nov-2009 15
CONTRACT-81352 2009 Dec-2009 16
CONTRACT-81352 2010 Jan-2010 17
CONTRACT-81352 2010 Feb-2010 18
CONTRACT-81352 2010 Mar-2010 19
CONTRACT-81352 2010 Apr-2010 20
CONTRACT-81352 2010 May-2010 21
CONTRACT-81352 2010 Jun-2010 22
CONTRACT-81352 2010 Jul-2010 23
CONTRACT-81352 2010 Aug-2010 24
CONTRACT-81352 2010 Sep-2010 25
CONTRACT-81352 2010 Oct-2010 26
CONTRACT-81352 2010 Nov-2010 27
CONTRACT-81352 2010 Dec-2010 28
CONTRACT-81352 2011 Jan-2011 29
CONTRACT-81352 2011 Feb-2011 30
CONTRACT-81352 2011 Mar-2011 31
CONTRACT-81352 2011 Apr-2011 32
CONTRACT-81352 2011 May-2011 33
CONTRACT-81352 2011 Jun-2011 34
CONTRACT-81352 2011 Jul-2011 35
CONTRACT-81352 2011 Aug-2011 36
CONTRACT-81352 2011 Sep-2011 37
My desired result is to have a 1 in the YEAR_NUM column for 2008, 2 in the YEAR_NUM column for 2009, 3 in the YEAR_NUM column for 2010 and 4 in the YEAR_NUM column for 2011.
Obviously, it is doing a row_number() based off the period as well but when removing the period and keeping my groupins within SQL, I get the desired results (kind of). Essentialy, removing the period fixes the problem. I then have 4 lines and 1, 2, 3, and 4 sequentially in the YEAR_NUM column. However, I must display the period as well, but I don't want that to affect/impact by row_number partition by statement. So ultimately for the first four rows, it should all say 1 in the YEAR_NUM.
Any SQL advice is greatly appreciated.
Thanks!