4
Hello, community!
I need an SQL query with the values of a SQL Server 2012 table field shown as frequency distribution.
Let’s say I have, today, the field Sales (in R$), type float.
The lowest sales recorded was R$ 0.01 and the highest was R$ 1,000,345.23.
The detail is that I’ll never know which one is bigger and which one is smaller.
It’s hundreds of sales. I want to return class frequencies. Each class will have a range of R$100,000.00.
Then my results would have to come in the form of a frequency distribution.
Thus,
The SQL Server 2012 table loads the sales values:
The query brings the values distributed in range:
I think something more or less like this solves, if you want to group by 0-9,10-99,100-999, etc:
GROUP BY FLOOR( LOG( valor ) / LOG( 10 ) + 1 )
. If you just want to divide by 1000000, 2000000 etcGROUP BY FLOOR( valor / 1000000 )
.– Bacco
The time SQL Fiddle decides to work I put more complete, in answer form.
– Bacco
T-SQL has LOG10( value ) also...
– Bacco
I did it, @Bacco! With your help, of course!!! In fact, I came to a page that explains everything right: http://sqlsunday.com/2014/09/14/histogram-in-t-sql/. I’m going to post the code.
– StillBuggin