Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Histogram

Author  Topic 

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-11-05 : 09:44:18
Hi!

Wonder if someone knows how to calculate histogram in sql query?
I know how to do it mathematicly but not in a single query.
Can it been done ?

Any tips/ideas

Thanks
V

shebert
Yak Posting Veteran

85 Posts

Posted - 2010-11-05 : 10:50:23
How do you do it mathmaticaly?
Go to Top of Page

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-11-06 : 17:45:19
quote:
Originally posted by shebert

How do you do it mathmaticaly?



Hi Sherbert!

Sorry not to answer so quickly on your good questions.
Unfortunate i will have to jump over to answer all the details how to do it mathematics for the moment, and show how i have "been" solve it. Its not particular dynamic

Here is the base:

Lets say you have an table T with values where the min-value is 0 and max-value that is 25. And we want it now to be classified in 5 bars,

The interval would be:

I = (25 - 0) / 5 = 5

Formula:
(max_value - min_value) / number_of_bars

Than we could write something like

select count(A) AS Bar_0_5 from T where A >= 0 AND A < 5
union
select count(A) AS Bar_5_15 from T where A >= 5 AND A < 10
union
select count(A) AS Bar_10_15 from T where A >= 10 AND A < 15
union
select count(A) AS Bar_15_20 from T where A >= 15 AND A < 20
union
select count(A) AS Bar_20_25 from T where A >= 20 AND A <= 25

note: the interval here has 5 steps on each bar.

It counts every single value from that table T where in the field A has an certain value that close up to an certain bars.

very simple.

Now, the though here is to do it more dynamic, so it can be reusable.
That is the problem.

Hope someone could figured out, if it can been done.
I have an suggestion for an solutions, but it doesn't really looks good.


thx
V
Go to Top of Page
   

- Advertisement -