Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

t-sql

Status
Not open for further replies.

fmian20

Programmer
Jul 17, 2004
1
GB
Id Numb
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
.. ..
.. ..
.. ..
.. .


Say I want to select max and min numb and there ids from above tabl in single select statement e.x select min(numb) ,id --(id for that (min (numb)))
,max(numb),id --(id for that (max (numb)))
from tabl --actual table is consist of 100000 number so how can we achieve it in single select statement.

Question2nd:
If I want sum for first five numb and sum for next 5 numb, and sum of next 5 numb ….so on… and then sum of those sums and there id
How it can be done

Bit unusual question but see how we go?
Fmian20@hotmail.com
Mian.fareed@bet365.com



 
select
maxid = t1.id, maxnumb = t1.numb ,
minid = t2.id, minnumb = t2.numb
minid = (select top 1 id from tbl order by numb) ,
from tbl t1 , tbl t2
where t1.id = (select top 1 id from tbl order by numb desc)
and t2.id = (select top 1 id from tbl order by numb)






======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top