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 2000 Forums
 Transact-SQL (2000)
 % change calculation in group by with dates

Author  Topic 

Champinco
Yak Posting Veteran

54 Posts

Posted - 2006-11-26 : 18:58:07
Hi guys, could i please get some assistance with the below:

Here is the table/ouput I require, particularly the last column %_change_prev_month:

Payment_point Payment_type month amount %_change_prev_month
AA000 FLAG 1/04/2006 110 NO PREVIOUS MONTH
AA000 FLAG 1/05/2006 100 -10%
AA000 FLAG 1/06/2006 100 0%
AA000 FLAG 1/07/2006 110 9%
AA000 US 1/01/2005 600 NO PREVIOUS MONTH
AA000 US 1/02/2005 300 -100%
AA000 BON 1/03/2005 20 NO PREVIOUS MONTH
AA000 BON 1/04/2005 30 33%
etc... (including other payment_points)

This is my current query that has the structure as above, however I need assistance with the ****** calculation which is the column %_change_prev_month.
That is firstly check each row if there is a previous month by payment_type, if there isn't then no % calculation can be done (i.e: leave blank or leave a 'no previous month message'),
secondly if there is a previous month by payment type (for that payment_point) then calculate the % change in amount value.
Cheers, im not sure if the query below is the right way to go? i might need a nested query or if/else?

select top 100000 payment_point,
payment_type,
month,
sum(amount) as amount,
****** as %_change_prev_month
from table1
--where payment_type = 'AAA00'
group by payment_point, payment_type, month
order by payment_point, payment_type, month

GK

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-27 : 01:16:33
This is how you do it. You also need to brush up your math skills regarding percentual changes.
-- prepare test data
declare @t table (Payment_point varchar(5), Payment_type varchar(4), [month] datetime, amount smallint)

set dateformat dmy

insert @t
select 'AA000', 'FLAG', '1/04/2006', 110 union all
select 'AA000', 'FLAG', '1/05/2006', 100 union all
select 'AA000', 'FLAG', '1/06/2006', 100 union all
select 'AA000', 'FLAG', '1/07/2006', 110 union all
select 'AA000', 'US', '1/01/2005', 600 union all
select 'AA000', 'US', '1/02/2005', 300 union all
select 'AA000', 'BON', '1/03/2005', 20 union all
select 'AA000', 'BON', '1/04/2005', 30

-- do the work
select t.Payment_point,
t.Payment_type,
t.[month],
t.amount,
CASE
WHEN v.amount IS NULL THEN 'No previous month'
else str(100.0 * (t.[amount] - v.[amount]) / v.[amount], 17, 2)
end '%_change_prev_month'
from @t t
left join @t v on v.payment_point = t.payment_point
and v.payment_type = t.payment_type
and year(v.[month]) = year(t.[month])
and month(v.[month]) = month(t.[month]) - 1
order by t.Payment_point,
t.Payment_type,
t.[month]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-27 : 01:25:21
How do you break years? Does the percentual change reset to 'No previous month'?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -