Monday, March 19, 2012

Keyfunctions

ColumnX holds values between 1001 and 5999. I want a result set such as below:
Group Quantity
1000s xxx
2000s xxx
3000s xxx
4000s xxx
5000s xxx

i.e. 1000s are numbers that start with 1___.

I don't want from you exact code. Please just say keywords that i should use in this query.Whisper [:-*] Such as count, cast, case, between etc.

This is a crude way and someone has a better solution but one way you could do if you knew all the groups is :

SELECT[Group]='1000s',SUM(ColX)as NewtotalFROMYourTableWHEREcolval >= 1000and colval < 2000UNIONALLSELECT[group]='2000s',SUM(ColX)as newtotalFROMYourTableWHEREcolval >= 2000and colval < 3000

|||

Thank you. In fact i just wanted keywords. =) Anyway, what is colval?

Invalid column name 'colval'

Another solution that works well:

select min((colA / 1000) * 1000), count(*)
from table1
group by colA / 1000
order by min((colA / 1000) * 1000);

|||colval is same as colA in your solution..but your solution is great..

No comments:

Post a Comment