Jump to content

Archived

This topic is now archived and is closed to further replies.

nickspick

Grouping by numbers

Recommended Posts

Hi,

I've got a table with a column that stores prices that vary considerably. These prices will be used to create options for the user to select a price range. Rather than presenting every number I want to group the numbers into ranges and display these along with a count of how many results fall into each range.

I've been able to achieve the same concept with datestamps (See example) but am stumped with what to use for just plain numbers.
[pre]
[color=blue]SELECT
DATE_FORMAT(FROM_UNIXTIME(date), '%Y') as new_date,
COUNT(*) AS numRows
FROM
table
GROUP BY
new_date[/color]
[/pre]
The above example will group all timestamps into years and also count how records are contain within each group. I'd like a similar thing for numbers but grouping them into 1000s or 10000s.

Any ideas?

Many thanks!

Share this post


Link to post
Share on other sites
You would want to use the option [between] on your select query statement

[code=php:0]
$query = "select * from pricing where (Mcov between $Minprice and $Maxprice)";
[/code]

Share this post


Link to post
Share on other sites
And if you use that with a proper min/max/ceil/floor function, you can get MySQL to dump out the range as well.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.