| Author |
Topic |
|
thiyait
Yak Posting Veteran
70 Posts |
Posted - 2007-05-22 : 02:20:54
|
Hi alli have queryselect col1,col2 from table1i need to append the incremental value to the col2 for each rowHere is the table data:-col1, col2 Expecting query result like belowcol1, col2---------0001 aus10002 usa20003 Bel3 Any one help me...........Thanks in advancethiya |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2007-05-22 : 02:29:38
|
| the "incremental value" is the value returned in col1 correct?-ec |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-22 : 02:31:12
|
| Where do you want to show your data?If you use front end application, you can easily do numbering thereor if you want to append col1 values to col2, then try usingSelect col1, col2+cast(cast(col1 as int) as varchar(4)) from tableMadhivananFailing to plan is Planning to fail |
 |
|
|
thiyait
Yak Posting Veteran
70 Posts |
Posted - 2007-05-22 : 02:44:23
|
Hi eyechart & madhivananThanks for quick reply, i didnt mean to concatinate the first column with second column.i want display to front end applicaition with appended incremental value in column 2like this:col1, col2---------0235 aus10023 usa20345 Bel3 0345 har4 Thanks in advancethiya |
 |
|
|
thiyait
Yak Posting Veteran
70 Posts |
Posted - 2007-05-22 : 02:47:27
|
hey, ihave missed the table data in previous replay This is the table valuescol1, col2---------0235 aus0023 usa0345 Bel 0345 har expected result like thiscol1, col2---------0235 aus10023 usa20345 Bel3 0345 har4 Thanks in advancethiya |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-22 : 02:52:55
|
| What is the order for incrementing the value?Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-22 : 02:57:58
|
quote: Originally posted by thiyait Hi eyechart & madhivananThanks for quick reply, i didnt mean to concatinate the first column with second column.i want display to front end applicaition with appended incremental value in column 2like this:col1, col2---------0235 aus10023 usa20345 Bel3 0345 har4 Thanks in advancethiya
What is your front end application?Here is the general approachDim Sno as integerSno=1While not Rs.eof print Rs("col1") print Rs("col2")+Sno Sno=Sno+1 Rs.moveNextLoopMadhivananFailing to plan is Planning to fail |
 |
|
|
thiyait
Yak Posting Veteran
70 Posts |
Posted - 2007-05-22 : 03:16:25
|
| HI Madhivanan, Thanks,i would like to have in single queryThanks in advancethiya |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-22 : 03:56:11
|
| <<i want display to front end applicaition with appended incremental value in column 2>>If the above is the case, then the code I have given is front end codeYou didnt answer to my question on which your front end application is.When it can be done easily in your front end, then why do you want to do it in query?MadhivananFailing to plan is Planning to fail |
 |
|
|
thiyait
Yak Posting Veteran
70 Posts |
Posted - 2007-05-22 : 10:38:19
|
Hi,I have given the part of requirement in the query..table has more than lakhs of record.its not wise to manipulate on frontend for such huge amountSo,please any one help me to solve this issue.thanks in advancethiya |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2007-05-22 : 10:52:16
|
quote: Originally posted by thiyait Hi,I have given the part of requirement in the query..table has more than lakhs of record.its not wise to manipulate on frontend for such huge amountSo,please any one help me to solve this issue.thanks in advancethiya
try this ...Create Table #Temp(Num Numeric,TName VarChar(10))Insert Into #Temp Values (111, 'A')Insert Into #Temp Values (222, 'B')Insert Into #Temp Values (333, 'C')Insert Into #Temp Values (444, 'D')Select Num, TName + Convert(VarChar(3), SrNo) As New_Col From (Select (Select Count(*) From #Temp As A Where A.Num <= B.Num) As SrNo, Num, TName From #Temp As B) As TestDrop Table #TempMahesh |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-22 : 11:00:24
|
quote: Originally posted by mahesh_bote
quote: Originally posted by thiyait Hi,I have given the part of requirement in the query..table has more than lakhs of record.its not wise to manipulate on frontend for such huge amountSo,please any one help me to solve this issue.thanks in advancethiya
try this ...Create Table #Temp(Num Numeric,TName VarChar(10))Insert Into #Temp Values (111, 'A')Insert Into #Temp Values (222, 'B')Insert Into #Temp Values (333, 'C')Insert Into #Temp Values (444, 'D')Select Num, TName + Convert(VarChar(3), SrNo) As New_Col From (Select (Select Count(*) From #Temp As A Where A.Num <= B.Num) As SrNo, Num, TName From #Temp As B) As TestDrop Table #TempMahesh
Never do this type of Serilazation using sqlIf there are thousands of millions of rows in the table, the query will take long time to finishMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-22 : 11:06:27
|
quote: Originally posted by thiyait Hi,I have given the part of requirement in the query..table has more than lakhs of record.its not wise to manipulate on frontend for such huge amountSo,please any one help me to solve this issue.thanks in advancethiya
Is there a case when you need to show lakhs of records in front end?Which is your front end and how you are sending data to it?How will you display data in front end?Why do you think generating serial no at front end is difficult and ineffecient?It is very very easy to show data with serial no in front endThere is no point in doing this in sql if you want to show them in front endMadhivananFailing to plan is Planning to fail |
 |
|
|
thiyait
Yak Posting Veteran
70 Posts |
Posted - 2007-05-23 : 06:10:25
|
| Hi mahesh,Thanks for the reply,i have one more question on thisif first column not be the numeric.how to solve this.col1 col2----------asdf Agfds Blkjh C ouyt DMathi,people here using the own custom made framework to display the data. There is no option for front end manipulationhope now you clear |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-23 : 06:29:04
|
| <<people here using the own custom made framework to display the data>>What framework is that?When data are displayed using framework, isnt it possible simple add row id there as I have specified in my second reply?MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-23 : 06:35:06
|
quote: Originally posted by thiyait Hi mahesh,Thanks for the reply,i have one more question on thisif first column not be the numeric.how to solve this.col1 col2----------asdf Agfds Blkjh C ouyt DMathi,people here using the own custom made framework to display the data. There is no option for front end manipulationhope now you clear
As you use SQL Server 2005, you can make use of row_number()Select col1,col2+cast(row_id as varchar(5)) as col2 from(select col1,col2,row_number() over (order by col1 asc) as row_id from yourtable)TMadhivananFailing to plan is Planning to fail |
 |
|
|
|