SQLServerCentral » SQL Server 2008 » T-SQL (SS2K8) » Histogram chart in sqlInstantForum 2016-2 FinalSQLServerCentralhttps://www.sqlservercentral.com/Forums/SQLServerCentralThu, 27 Jul 2017 04:00:29 GMT20Histogram chart in sqlhttps://www.sqlservercentral.com/Forums/FindPost1408210.aspxHi,
Im hoping someone is able to check what Ive created for a histogram chart and can provide feedback, suggestions, improvements.
(Im pretty sure there will be a few).
I decided to write my own for a business requirement and also after reading this great article from Dwain.
[url]http://www.sqlservercentral.com/articles/Excel/91179/[/url]
As everyone keeps saying if you don't understand the code then don't use it (I didn't understand enough of Dwains code to be comfortable in using it in prod).
Recursive cte's are something Ive never required and really don't have the know how in them just yet.
Ive borrowed (ok stolen - thanks Dwain!) Dwains initial setup code and removed all the items except those relating to float
[code="sql"]DECLARE @NumberOfRows INT
,@FStartValue FLOAT
,@FEndValue FLOAT
,@FRange FLOAT
SELECT @NumberOfRows = 10000
-- -- Random FLOATs on the closed interval {275, 850}
,@FStartValue = 275
,@FEndValue = 850
,@FRange = @FEndValue - @FStartValue
DECLARE @MyTestTable TABLE (
RandomFloat FLOAT
)
INSERT INTO @MyTestTable
SELECT TOP (@NumberOfRows)
RandomFloat = RAND(CHECKSUM(NEWID())) * @FRange + @FStartValue
FROM sys.all_columns a1
CROSS JOIN sys.all_columns a2[/code]
So here is my code - I needed the outer join to the Tally table so I could get a guaranteed unbroken (no gaps) list of bins.
Im hoping the code is self explanatory but if needed (asked for) I will add in some comments.
[code="sql"]declare @bins as int = 40
declare @interval as float
declare @min as float
declare @max as float
select @interval = (max(RandomFloat) - min(RandomFloat))/@bins, @min = min(RandomFloat), @max = max(RandomFloat)
from @MyTestTable
;with cteHistogram (N, StartBin, EndBin, TotalCount)
as
(
select
N, @min+(@interval*(N-1)) StartBin, @min+(@interval*N) EndBin, coalesce(count(RandomFloat), 0) TotalCount
from
@MyTestTable cross join Tally
where
RandomFloat >= @min+(@interval*(N-1)) and RandomFloat < @min+(@interval*N) and n <= @bins-1
group by
N
union all
select
N, @min+(@interval*(N-1)) StartBin, @min+(@interval*N) EndBin, coalesce(count(RandomFloat), 0) TotalCount
from
@MyTestTable cross join Tally
where
RandomFloat >= @min+(@interval*(N-1)) and RandomFloat <= @max and n = @bins
group by
N
)
select
Tally.N, @min+(@interval*(Tally.N-1)) StartBin, case when Tally.N = @bins then @max else @min+(@interval*Tally.N) end EndBin, coalesce(TotalCount, 0) TotalCount
from
Tally left outer join cteHistogram on Tally.N = cteHistogram.N
where
Tally.N <= @bins
order by
Tally.N[/code]
ps Im a beginner in Tally tables but have recently started using them quite a bit and absolutely love them.
Im also assuming everyone has a Tally table called "Tally" or can make one easily enough.Thu, 17 Jan 2013 18:03:53 GMTmatakRE: Histogram chart in sqlhttps://www.sqlservercentral.com/Forums/FindPost1408686.aspxThanks Chris,
Appreciate the feedback.
Looks like i was close so thats always a bonus.
Ive never used ntile before and it certainly looks interesting (although its not suitable for this requirement).
Initially i think i will use your first suggestion and see how that goes.
Thanks (and to dwain for the setup code - will come in handy)Thu, 17 Jan 2013 18:03:53 GMTmatakRE: Histogram chart in sqlhttps://www.sqlservercentral.com/Forums/FindPost1408682.aspx[quote][b]matak (1/17/2013)[/b][hr]
I decided to write my own for a business requirement and also after reading this great article from Dwain.
[url]http://www.sqlservercentral.com/articles/Excel/91179/[/url]
As everyone keeps saying if you don't understand the code then don't use it (I didn't understand enough of Dwains code to be comfortable in using it in prod).
Recursive cte's are something Ive never required and really don't have the know how in them just yet.
Ive borrowed (ok stolen - thanks Dwain!) Dwains initial setup code and removed all the items except those relating to float[/quote]
You're welcome! Looks like ChrisM has got ya covered.Thu, 17 Jan 2013 17:39:56 GMTdwain.cRE: Histogram chart in sqlhttps://www.sqlservercentral.com/Forums/FindPost1408291.aspxIt looks ok to me, apart from UNION picking up the 40th bin. When this is done, the query is quite efficient:
[code="sql"];with cteHistogram (N, StartBin, EndBin, TotalCount)
as
(
select
N,
@min+(@interval*(N-1)) StartBin,
@min+(@interval*N) EndBin, coalesce(count(RandomFloat), 0) TotalCount
from @MyTestTable
cross join Tally
where RandomFloat >= @min+(@interval*(N-1))
and ((RandomFloat < @min+(@interval*N) and n <= @bins-1)
OR (RandomFloat <= @max and n = @bins))
group by N
)
select
Tally.N, @min+(@interval*(Tally.N-1)) StartBin, case when Tally.N = @bins then @max else @min+(@interval*Tally.N) end EndBin, coalesce(TotalCount, 0) TotalCount
from
Tally left outer join cteHistogram on Tally.N = cteHistogram.N
where
Tally.N <= @bins
order by
Tally.N [/code]
Note that min and max are the calculated range min and max values, rather than RandomFloat values. If you want RandomFloat values, try this:
[code="sql"]-------------------------------------------------------------------------
-- calculate the range start and end for each bin
;WITH Bins AS (
SELECT
t.n,
RangeStart = minval + (interval * (t.n-1)),
RangeEnd = CASE WHEN t.n = @bins THEN maxval+1 ELSE minval + (interval * t.n) END
FROM (
SELECT
interval = (MAX(RandomFloat) - MIN(RandomFloat)) / @bins,
minval = MIN(RandomFloat),
maxval = MAX(RandomFloat)
FROM @MyTestTable
) d
CROSS JOIN Tally t
WHERE t.n <= @bins
)
-- Summarise the bin contents. Min and max are actual bins.
SELECT
b.N,
StartBin = MIN(RandomFloat),
EndBin = MAX(RandomFloat),
RangeStart = MIN(b.RangeStart),
RangeEnd = MIN(b.RangeEnd),
TotalCount = COUNT(*)
FROM Bins b
INNER JOIN @MyTestTable t
ON t.RandomFloat >= b.RangeStart AND t.RandomFloat < b.RangeEnd
GROUP BY N [/code]
Now, if your requirement was to split the population into 40 equal groups and summarise those groups, you could use NTILE, like this:
[code="sql"]-- split the set into (@bins) roughly equal-sized populations
SELECT
N,
StartBin = MIN(RandomFloat),
EndBin = MAX(RandomFloat),
TotalCount = COUNT(*)
FROM (
SELECT
RandomFloat,
N = NTILE(@bins) OVER(ORDER BY (RandomFloat))
FROM @MyTestTable
) d
GROUP BY N[/code]Thu, 17 Jan 2013 03:42:05 GMTChrisM@Work