Quantcast
Channel: Answers by "Kevin Feasel"
Viewing all articles
Browse latest Browse all 15

Answer by Kevin Feasel

$
0
0
Here's a slightly modified example with some data attached: declare @t1 table ( AcctId int, SiteId int, Country char(2), ItemId int, UsageDate date ); insert into @t1 values (123, 321, 'US', 69, '2011-05-15'), (123, 321, 'US', 73, '2011-05-15'), (123, 321, 'US', 69, '2011-05-16'), (123, 456, 'FR', 76, '2011-05-18'), (321, 789, 'US', 53, '2011-05-17'), (123, 321, 'US', 69, '2011-05-05'), (123, 321, 'US', 73, '2011-05-05'), (123, 321, 'US', 69, '2011-05-06'), (123, 456, 'FR', 76, '2011-05-08'), (321, 789, 'US', 53, '2011-05-07'), (123, 321, 'US', 69, '2011-06-15'), (123, 321, 'US', 73, '2011-06-15'), (123, 321, 'US', 69, '2011-06-16'), (123, 456, 'FR', 76, '2011-06-18'), (321, 789, 'US', 53, '2011-06-17'), (123, 321, 'US', 69, '2011-04-15'), (123, 321, 'US', 73, '2011-04-15'), (123, 321, 'US', 69, '2011-04-16'), (123, 456, 'FR', 76, '2011-04-18'), (321, 789, 'US', 53, '2011-04-17'); with records as ( select AcctId, Country, MONTH(UsageDate) as mon, COUNT(distinct UsageDate) as Count from @t1 group by AcctId, Country, MONTH(UsageDate) ) select rCurrent.AcctId, rCurrent.Country, rCurrent.mon as CurrentYearMonth, rCurrent.Count as CurrentMonthTotal, rPrevious.mon as PreviousYearMonth, rPrevious.Count as PreviousMonthCount, CAST( 100 * (rCurrent.Count * 1.0 / NULLIF(rPrevious.Count, 0) - 1) as decimal (10, 2)) AS DifferenceInPercent from records rCurrent left outer join records rPrevious on rCurrent.AcctId = rPrevious.AcctId and rCurrent.Country = rPrevious.Country Could you perhaps give an example of data which is being aggregated incorrectly?

Viewing all articles
Browse latest Browse all 15

Trending Articles