0

I have some code borrowed from a previous stackoverflow post which makes it easy to create histogram data out of a large table. I repeat it below:

SELECT ROUND(numeric_value, -2)    AS bucket,
       COUNT(*)                    AS COUNT,
FROM   my_table
GROUP  BY bucket;

[I removed references to the BAR column as I don't need it.] The problem is it is only possible to vary the bucket size by one order of magnitude (by changing the ROUND attribute), how would I change the code to enable me to change the bucket size with more flexibility? For example, if I wanted to state the numerical size of each bucket?

Thanks!

2
  • The post you linked has an answer that does that. (I'd only use the select part the GROUP BY looks suspect). Commented Oct 24, 2014 at 14:20
  • This doesn't work. Could it be because my numerical values are all decimals between 0 and 1, not integers? Commented Oct 24, 2014 at 15:51

1 Answer 1

0

E.g. for a 0.2-step bucket do a

SELECT 
    (numeric_value DIV 0.2) * 0.2 as bucket,
    Count(*)
FROM 
    mytable
GROUP BY 1
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work. Could it be because my numerical values are all decimals between 0 and 1, not integers?
For decimal values do eg a ... (numeric_value DIV 0.1) * 0.1 as bucket ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.