| Author |
Topic |
|
almir
Starting Member
10 Posts |
Posted - 2007-10-06 : 10:28:09
|
| HiI have a pretty complex select statement using UNION to pull data from 2 tables.Something like this:(Select field1,field2 from t1 where field1 = @param1) UNION(Select field1,field2 from t2 where field2 = @param1) Order by field1It is much more complex than this but you get the picture. I would like to add unique identifier for each row with Row_Number function based on ordering field similar to the line bellow:ROW_NUMBER() OVER(Order by field1 ASC) as rowid,If I add them within respective select statements they will all generate their own sequence resulting in rowid duplicate values.Is it possible to generate unique rowid for each row?Your help will be greatly appreciated |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-10-06 : 10:31:58
|
| select ROW_NUMBER() OVER(Order by field1 ASC) as rowid, <otherColumns>from ((Select field1,field2 from t1 where field1 = @param1) UNION(Select field1,field2 from t2 where field2 = @param1) ) t1Order by field1_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
 |
|
|
almir
Starting Member
10 Posts |
Posted - 2007-10-06 : 11:49:06
|
| Thanx spirit You put me on the right track, I was playing with subqueries but since the statement is dynamically constructed I had an error that was difficult to debug. Everything works ok now. The purpose of whole exercise is paging of the result set.I'll be passing soon through Slovenia on my way to UK Ptuj - Maribor - Ostereich grenz |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-10-06 : 12:41:48
|
| car or train?_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
 |
|
|
almir
Starting Member
10 Posts |
Posted - 2007-10-06 : 12:48:51
|
| car |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-10-06 : 12:53:06
|
too bad i live in the capital which is a bit way off... we could go grab a beer _______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
 |
|
|
almir
Starting Member
10 Posts |
Posted - 2007-10-06 : 12:59:55
|
| Since I don't drink I would have a coke, but you never know I might make detour through Northern Italy and France to show my family different side of Europe :-) |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-10-06 : 13:13:10
|
coke's ok too well if you decide to go through ljubljana drop me mail._______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-08 : 04:24:26
|
Is L silent in ljubljana? MadhivananFailing to plan is Planning to fail |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-10-08 : 04:30:25
|
| nope._______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-08 : 06:18:41
|
Then I wonder how I can pronounce it MadhivananFailing to plan is Planning to fail |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
|
AJA
Starting Member
3 Posts |
Posted - 2009-08-18 : 08:32:42
|
| Hi spirit1,I am wandering for a better option, don't you think using a subquery will degrade the performance.I have to again select the data from the resultset for paging using the Row_Number() I have generated, but it costs much because first I have a Union then I also have subquery and finally i am returning the result.Please suggest if i can do better in case.Thanks in advance.I wish, I wasn't so nice ! |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2009-08-18 : 08:35:25
|
| Please start a new thread for your question.in there post your example code.___________________________________________________________________________Causing trouble since 1980Blog: http://weblogs.sqlteam.com/mladenpSpeed up SSMS development: www.ssmstoolspack.com <- version 1.5 out! |
 |
|
|
AJA
Starting Member
3 Posts |
Posted - 2009-08-18 : 09:04:58
|
| Sprint1, I have started new thread "Row_Number and union".please suggest.I wish, I wasn't so nice ! |
 |
|
|
|