| Author |
Topic |
|
ch.rajesh
Starting Member
7 Posts |
Posted - 2009-04-13 : 01:31:50
|
| Hi All I have one query ie, I have a table say "MstRequest". where I have Primary key for Column "RequestId" and it has identity on it.Here I have "CreatedDate" column also. So i want to get my data based on year ie every year "RequstId" should start from "1". Let that column be a new one.ex: ReqId ---- CreatedDate----- "MyFormat" 1 ----- 1/1/09 ---- 1 2 ----- 2/2/09 ---- 2 3 ----- 2/3/09 ---- 3 4 ----- 2/2/10 ---- 1 5 ----- 2/2/10 ---- 2 6 ----- 2/3/10 ---- 3 7 ----- 2/2/11 ---- 1 8 ----- 2/4/11 ---- 2In this way so kindly send me the solution if any one knows. very urgent. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-13 : 02:41:59
|
quote: Originally posted by ch.rajesh Hi All I have one query ie, I have a table say "MstRequest". where I have Primary key for Column "RequestId" and it has identity on it.Here I have "CreatedDate" column also. So i want to get my data based on year ie every year "RequstId" should start from "1". Let that column be a new one.ex: ReqId ---- CreatedDate----- "MyFormat" 1 ----- 1/1/09 ---- 1 2 ----- 2/2/09 ---- 2 3 ----- 2/3/09 ---- 3 4 ----- 2/2/10 ---- 1 5 ----- 2/2/10 ---- 2 6 ----- 2/3/10 ---- 3 7 ----- 2/2/11 ---- 1 8 ----- 2/4/11 ---- 2In this way so kindly send me the solution if any one knows. very urgent.
Refer point 2http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-04-13 : 02:43:12
|
| Hi try this once,select *,row_number() over ( partition by right(createddate,2) order by reqid ) as Myformat from @temp |
 |
|
|
ch.rajesh
Starting Member
7 Posts |
Posted - 2009-04-13 : 03:41:22
|
quote: Originally posted by Nageswar9 Hi try this once,select *,row_number() over ( partition by right(createddate,2) order by reqid ) as Myformat from @temp
Thank you for the replyseems it is not working.see the following result it showscheck the third row for year '09' it gives 33 2008-03-19 11:27:53.000 16 2008-03-19 11:27:53.000 214 2009-04-02 11:36:01.000 323 2011-04-09 01:30:04.000 424 2011-04-09 04:58:43.000 525 2011-04-09 05:00:22.000 626 2012-04-09 05:11:19.000 7 |
 |
|
|
ch.rajesh
Starting Member
7 Posts |
Posted - 2009-04-13 : 03:48:20
|
| Hi NageswarThanks for the reply, Its working. |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-04-13 : 06:13:48
|
quote: Originally posted by ch.rajesh Hi NageswarThanks for the reply, Its working.
Welcome |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-04-13 : 06:15:03
|
| Hi try this one alsoselect *,( select count(1) from @temp where t.reqid >= reqid and right(createddate,2)= right(t.createddate,2)) AS MyFormat from @temp t |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-13 : 06:20:12
|
quote: Originally posted by Nageswar9 Hi try this once,select *,row_number() over ( partition by right(createddate,2) order by reqid ) as Myformat from @temp
If createddate is of datetime datatype, use year(createddate) instead of right(createddate,2)MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-13 : 06:21:26
|
quote: Originally posted by Nageswar9 Hi try this one alsoselect *,( select count(1) from @temp where t.reqid >= reqid and right(createddate,2)= right(t.createddate,2)) AS MyFormat from @temp t
This will become ineffecient if there are large number of data in the table.MadhivananFailing to plan is Planning to fail |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-04-13 : 06:26:53
|
| Ok , Madhivanan |
 |
|
|
ch.rajesh
Starting Member
7 Posts |
Posted - 2009-04-13 : 06:27:49
|
| Hi All againIf I want the same according to Financial yearHow should I get it? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-13 : 08:49:39
|
| http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=123708MadhivananFailing to plan is Planning to fail |
 |
|
|
|