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 |
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2007-12-12 : 09:00:51
|
| Gurus,How to get first and last records from a table.I need sql query for this scenario. No cursor please!ThanksKrishna |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2007-12-12 : 09:02:55
|
mmmmmm.....The first and last records issue again?First and last record based on what?Duane. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-12-12 : 09:03:18
|
use min() and max() on the columnselect *from yourtablewhere id = (select min(id) from yourtable) KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2007-12-12 : 09:04:47
|
| Thanks Khtan |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-12-12 : 09:05:15
|
| [code]select * from (Select Top 1 * from table order by somecol asc) t1union allselect * from (Select Top 1 * from table order by somecol desc) t2[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-12 : 09:21:19
|
quote: Originally posted by CSK Gurus,How to get first and last records from a table.I need sql query for this scenario. No cursor please!ThanksKrishna
There is no such thing in a tableSQL Server doesnt have First and Last functionsMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-12 : 09:22:44
|
quote: Originally posted by harsh_athalye
select * from (Select Top 1 * from table order by somecol asc) t1union allselect * from (Select Top 1 * from table order by somecol desc) t2 Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
orselect *from yourtablewhere id in (select min(id) from yourtable union all select max(id) from yourtable)order by idMadhivananFailing to plan is Planning to fail |
 |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2007-12-12 : 10:02:03
|
quote: select *from yourtablewhere id in (select min(id) from yourtable union all select max(id) from yourtable)order by idMadhivananFailing to plan is Planning to fail
This is Khtan's answer madhi. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-13 : 01:32:23
|
quote: Originally posted by CSK
quote: select *from yourtablewhere id in (select min(id) from yourtable union all select max(id) from yourtable)order by idMadhivananFailing to plan is Planning to fail
This is Khtan's answer madhi.
Read it again. Thats not the same MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|