﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / find increase/decrease amount / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 14:16:05 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: find increase/decrease amount</title><link>http://www.sqlservercentral.com/Forums/Topic1366344-392-1.aspx</link><description>[quote][b]sayedkhalid99 (10/2/2012)[/b][hr]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.....[/quote]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.</description><pubDate>Wed, 03 Oct 2012 07:39:26 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: find increase/decrease amount</title><link>http://www.sqlservercentral.com/Forums/Topic1366344-392-1.aspx</link><description>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 @tdeclare @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 &amp;gt; 50 it shows increase from first month so 400-50=+350 so --&amp;gt; AfterIncreaseDecreaseAmount=350(-300,100,3,'A'), -- for third month 02/03/2002  it is 100 &amp;lt; 400 (sum of 1st,2st month amount) so it shows decrease 100-400=-300 so--&amp;gt;AfterIncreaseDecreaseAmount=-300 (+200,200,1,'B'),--  for b we have 200            it is 200 &amp;gt; 0 ,if it is only one it shall count that as 0 so it shows increase 200-0=200 --&amp;gt; AfterIncreaseDecrease=200   (+300,500,2,'B'),-- for b we have 500 500 &amp;gt; 200--&amp;gt; 500-200=300 it show increased --&amp;gt;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</description><pubDate>Tue, 02 Oct 2012 22:53:26 GMT</pubDate><dc:creator>sayedkhalid99</dc:creator></item><item><title>RE: find increase/decrease amount</title><link>http://www.sqlservercentral.com/Forums/Topic1366344-392-1.aspx</link><description>[code="sql"] -- 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.sectorFROM @t crLEFT 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[/code]</description><pubDate>Tue, 02 Oct 2012 04:51:57 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: find increase/decrease amount</title><link>http://www.sqlservercentral.com/Forums/Topic1366344-392-1.aspx</link><description>[quote][b]sayedkhalid99 (10/1/2012)[/b][hr]the rule here is we have month no here.1. check secondmonth amount2. sum(second month amount) &amp;gt; 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) &amp;gt; 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.[/quote]I still can't follow that.Can you give the values for Sector A with the steps above?  That should make it clearer.</description><pubDate>Tue, 02 Oct 2012 02:54:23 GMT</pubDate><dc:creator>laurie-789651</dc:creator></item><item><title>RE: find increase/decrease amount</title><link>http://www.sqlservercentral.com/Forums/Topic1366344-392-1.aspx</link><description>[quote] I want to find the increase / decrease amount group by sector and month. [/quote]It is a lot easier if you post DDL for a base table, so we can see keys and constraints. We now have to guess at everything and correct your DDL to conform to ISO-11179 rules; your data element names are so generic as to be useless. This is called a delta, from the Greek letter that mathematicians use for changes Months are temporal values, not integers. I like to use the MySQL convention for months; you can Google it. CREATE TABLE Foobars(foo_id INTEGER NOT NULL,  sector_code CHAR(1), PRIMARY KEY (foo_id, sector_code),  foobar_qty INTEGER NOT NULL CHECK (foobar_qty &amp;gt; 0),  report_month CHAR(10) NOT NULL CHECK (report_month LIKE '[12][0-9][0-9][0-9]-[0-3][0-9]-00')); INSERT INTO Foobars(foo_id, foobar_qty, report_month , sector_code)VALUES(2, 'A', 50, '2012-01-00'), (3, 'B', 200, '2012-01-00'), (4, 'C', 300, '2012-01-00'), (5, 'A', 400, '2012-02-00'), (6, 'B', 500, '2012-02-00'), (7, 'C', 600, '2012-02-00'), (8, 'A', 100, '2012-03-00'), (9, 'B', 500, '2012-03-00'), (10, 'C', 100, '2012-03-00');Your specs and your sample output do not match. The first row has two sectors in one monthWITH Month_Totals (report_month, sector_code, month_qty_tot)AS(SELECT report_month, sector_code, SUM (foobar_qty)  FROM Foobars GROUP BY report_month, sector_code)SELECT report_month, sector_code, month_qty_tot,      ( month_qty_tot - LAG(month_qty_tot)       OVER (PARTITION BY sector_code 	             ORDER BY report_month ASC)) AS delta FROM Month_Totals ORDER BY sector_code, report_month FROM Month_Totals; </description><pubDate>Mon, 01 Oct 2012 17:24:36 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: find increase/decrease amount</title><link>http://www.sqlservercentral.com/Forums/Topic1366344-392-1.aspx</link><description>the rule here is we have month no here.1. check secondmonth amount2. sum(second month amount) &amp;gt; 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) &amp;gt; 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.</description><pubDate>Mon, 01 Oct 2012 05:27:29 GMT</pubDate><dc:creator>sayedkhalid99</dc:creator></item><item><title>RE: find increase/decrease amount</title><link>http://www.sqlservercentral.com/Forums/Topic1366344-392-1.aspx</link><description>What are the rules for calculating increase &amp; decrease?It's not obvious from the expected results.</description><pubDate>Mon, 01 Oct 2012 02:52:18 GMT</pubDate><dc:creator>laurie-789651</dc:creator></item><item><title>find increase/decrease amount</title><link>http://www.sqlservercentral.com/Forums/Topic1366344-392-1.aspx</link><description>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 Outputdeclare @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,</description><pubDate>Mon, 01 Oct 2012 01:34:37 GMT</pubDate><dc:creator>sayedkhalid99</dc:creator></item></channel></rss>