General discussion

Locked

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.

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?

This conversation is currently closed to new comments.

1 total post (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Answer

by texasconnelly In reply to question t-sql

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

Back to Software Forum
1 total post (Page 1 of 1)  

Related Discussions

Related Forums