Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
assafsn
Starting Member
8 Posts |
Posted - 2004-09-27 : 11:14:47
|
| Hello all.I wrote the following SQL query:SELECT dbo.test2.num, dbo.test2.age, LAST(dbo.test1.profit)FROM dbo.test1 RIGHT OUTER JOIN dbo.test2 ON dbo.test1.num = dbo.test2.num AND dbo.test1.age <= dbo.test2.ageGROUP BY dbo.test2.num, dbo.test2.ageORDER BY dbo.test2.num, dbo.test2.ageI cant use it on MS-SQL because is doesn't support the function 'LAST'. Does any one have any idea how to get the same results without using 'LAST' ?Thanks for your help. |
|
|
influent
Constraint Violating Yak Guru
367 Posts |
Posted - 2004-09-27 : 11:30:46
|
| Does this work?:SELECT dbo.test2.num, dbo.test2.age, TOP 1 dbo.test1.profitFROM dbo.test1 RIGHT OUTER JOIN dbo.test2 ON dbo.test1.num = dbo.test2.num AND dbo.test1.age <= dbo.test2.ageGROUP BY dbo.test2.num, dbo.test2.ageORDER BY dbo.test2.num, dbo.test2.age |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-09-27 : 11:37:43
|
influent: it won't work because you can't use top like thatSELECT dbo.test2.num, dbo.test2.age, min(dbo.test1.profit) -- or max depends what you wantFROM dbo.test1 RIGHT OUTER JOIN dbo.test2 ON dbo.test1.num = dbo.test2.num AND dbo.test1.age <= dbo.test2.ageGROUP BY dbo.test2.num, dbo.test2.ageORDER BY dbo.test2.num, dbo.test2.agebut i doubt it will work. and how do you define last? last based on what?some table definitions and what are you trying to do will help...Go with the flow & have fun! Else fight the flow |
 |
|
|
assafsn
Starting Member
8 Posts |
Posted - 2004-09-27 : 12:24:23
|
| I can't use max or min because the field is varchar. I define the last based on on the max age in that grouping.Any new ideas ??? |
 |
|
|
influent
Constraint Violating Yak Guru
367 Posts |
Posted - 2004-09-27 : 13:14:37
|
| Are you just trying to get one row? I don't understand what you're trying to do that LAST does. |
 |
|
|
assafsn
Starting Member
8 Posts |
Posted - 2004-09-27 : 13:36:44
|
| Lets take these tables as example:test1:Name Age Classx 1 ax 4 ax 6 btest2:Name Agex 1x 2x 3x 4x 5x 6x 7x 8x 9x 10The desierable sql will give the following output:Name Age Classx 1 ax 2 ax 3 ax 4 ax 5 ax 6 bx 7 bx 8 bx 9 bx 10 b |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-09-27 : 14:19:21
|
| http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=40546- Jeff |
 |
|
|
|
|
|
|
|