SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


find increase/decrease amount


find increase/decrease amount

Author
Message
sayedkhalid99
sayedkhalid99
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 535
i want to find the increase / decrease amount group by sector and month.
--input table
declare @t table
([id] int,[Amount] int,monthno int,Sector char(1));
INSERT INTO @t
([id],[Amount],Monthno ,sector )
VALUES
(1, 100,1,'A'),
(2,50,1,'A'),
(3, 200,1,'B'),
(4, 300,1,'C'),
(5, 400,2,'A'),
(6, 500,2,'B'),
(7, 600,2,'C'),
(8, 100,3,'A'),
(9, 500,3,'B'),
(10, 100,3,'C')
select * from @t

-- Required Output

declare @output table ([IncreaseDecreaseAmount] int,Amount int,[Monthno]int,sector char(1))
INSERT INTO @output
([IncreaseDecreaseAmount],Amount,[Monthno],sector )
VALUES
(50,100,1,'A'),
(300,400,2,'A'),
(-200,100,3,'A'),
(200,200,1,'B'),
(300,500,2,'B'),
(0,500,3,'B')
select * from @output

regards,
laurie-789651
laurie-789651
SSChasing Mays
SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)

Group: General Forum Members
Points: 618 Visits: 1272
What are the rules for calculating increase & decrease?

It's not obvious from the expected results.
sayedkhalid99
sayedkhalid99
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 535
the rule here is we have month no here.

1. check secondmonth amount

2. sum(second month amount) > sum(first month amount)

3. get the difference

4. difference is greater then sum(first month amount) then it shows addition.

5. difference is less then sum(first month amount) then it shows reduction

6. check the amount for month3

7. sum of (First Month+second month) > sum of (third month)

8. difference is greater then sum(first+second month amount) then it shows addition.

9. difference is less then sum(first+second month amount) then it shows reduction


it shall be grouped by monthno and sector.
laurie-789651
laurie-789651
SSChasing Mays
SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)

Group: General Forum Members
Points: 618 Visits: 1272
sayedkhalid99 (10/1/2012)
the rule here is we have month no here.

1. check secondmonth amount

2. sum(second month amount) > sum(first month amount)

3. get the difference

4. difference is greater then sum(first month amount) then it shows addition.

5. difference is less then sum(first month amount) then it shows reduction

6. check the amount for month3

7. sum of (First Month+second month) > sum of (third month)

8. difference is greater then sum(first+second month amount) then it shows addition.

9. difference is less then sum(first+second month amount) then it shows reduction


it shall be grouped by monthno and sector.


I still can't follow that.

Can you give the values for Sector A with the steps above? That should make it clearer.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16406 Visits: 19554
 -- you have 2 rows for sector 'A', month 1
-- what do you want to do with them?
-- It's not clear from your "Required Output", which is incorrect
-- see row 3.
SELECT
[IncreaseDecreaseAmount] = ISNULL(cr.Amount-lr.Amount,cr.Amount),
cr.Amount,
cr.Monthno,
cr.sector
FROM @t cr
LEFT JOIN (
SELECT Sector, monthno, [Amount] = SUM([Amount])
FROM @t
GROUP BY Sector, monthno
) lr ON lr.Sector = cr.Sector AND lr.monthno+1 = cr.monthno
ORDER BY cr.sector, cr.Monthno



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
sayedkhalid99
sayedkhalid99
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 535
i have changed the my input and output and add date and comments to make it clear, for multiple entry with in a month it shall take the last entry.
declare @t table
([id] int,[Amount] int,Sector char(1),Currentdate datetime);
INSERT INTO @t
([id],[Amount],Sector,currentdate )
VALUES
(1, 100,'A','2002-01-01 00:00:00.000'),
(2,50,'A','2002-01-02 00:00:00.000'),
(3, 200,'B','2002-01-01 00:00:00.000'),
(4, 300,'C','2002-01-01 00:00:00.000'),
(5, 400,'A','2002-02-02 00:00:00.000'),
(6, 500,'B','2002-02-02 00:00:00.000'),
(7, 600,'C','2002-02-02 00:00:00.000'),
(8, 100,'A','2002-03-02 00:00:00.000'),
(9, 500,'B','2002-03-03 00:00:00.000'),
(10, 100,'C','2002-03-03 00:00:00.000'),
(11, 200,'C','2002-03-03 00:00:00.000')
select * from @t
declare @output table ([AfterIncreaseDecreaseAmount] int,Amount int,[Monthno]int,sector char(1))
INSERT INTO @output
([AfterIncreaseDecreaseAmount],Amount,[Monthno],sector )
VALUES
(50,100,1,'A'), -- for 01/01/2002 it is 100 , for 02/02/2002 for multiple entry with in the month it shall take the last amount based on max(id) or last(currentdate).
(+350,400,2,'A'), -- for second month 02/02/2002 it 400 > 50 it shows increase from first month so 400-50=+350 so --> AfterIncreaseDecreaseAmount=350
(-300,100,3,'A'), -- for third month 02/03/2002 it is 100 < 400 (sum of 1st,2st month amount) so it shows decrease 100-400=-300 so-->AfterIncreaseDecreaseAmount=-300
(+200,200,1,'B'),-- for b we have 200 it is 200 > 0 ,if it is only one it shall count that as 0 so it shows increase 200-0=200 --> AfterIncreaseDecrease=200
(+300,500,2,'B'),-- for b we have 500 500 > 200--> 500-200=300 it show increased -->AfterIncreaseDecrease=300
(0,500,3,'B'), -- for b in third month 500-500(sum of 1st,2stmonth amount of b) =0 AfterIncreaseDecrease=0
(+300,300,1,'C'),
(+300,600,2,'C'),
(400,100,3,'C')
select * from @output
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16406 Visits: 19554
sayedkhalid99 (10/2/2012)
i have changed the my input and output and add date and comments to make it clear, for multiple entry with in a month it shall take the last entry.
....


Please confirm that your desired results are correct for sector C, and add a few extra sample rows for B and C, including multiple rows for a single month. It will aid disambiguation. Thanks.

EDIT: more sample data requested.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search