Discussion Locked

question t-sql

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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?

1 reply

by texasconnelly

Question 1 - Try This:

select t1.numb as min_num, t1.id as min_id,
t2.numb as max_num, t2.id as max_id
from TABLE_NAME t1, TABLE_NAME t2,
(select min(numb) as theNumb from TABLE_NAME) theMin,
(select max(numb) as theNumb from TABLE_NAME) theMax
where t1.numb = theMin.theNumb
and t2.numb = theMax.theNumb

This discussion is closed to new replies.