General discussion


question t-sql

By fmian20 ·
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.

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?

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -


by texasconnelly In reply to question t-sql

Question 1 - Try This:

select t1.numb as min_num, as min_id,
t2.numb as max_num, as max_id
(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

Related Discussions

Related Forums