Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ Register Now and get your question answered!
 All Forums  SQL Server 2008 Forums  Transact-SQL (2008)  Calculate values Reply to Topic  Printer Friendly
Author  Topic

collie
Constraint Violating Yak Guru

400 Posts

 Posted - 11/13/2013 :  14:05:18 Hi,I have 2 tables. I need to calculate the values of the rows until the month/year where a deviation is found.In addition I need to calculate all the values where no deviation exists.```create table #Payments ( [ReceiptNo] int ,[LineNu] int ,[IdNo]int ,[Compensation]decimal (10,2) ,[Providence]decimal (10,2) ,[ForMonth]int ,[ForYear] int) create table #maxPremia (foryear int, maxPremia decimal (10,2)) insert into #maxPremia values (2011,1910.00) insert into #maxPremia values (2012,3456.00) insert into #payments values (1,1,222345,23,45,1,2011) insert into #payments values (1,2,222345,45,45,2,2011) insert into #payments values (2,2,222345,678.90,1234,3,2011) insert into #payments values (2,1,222345,124.0,8900,4,2011) insert into #payments values (2,3,222345,45,45,5,2011) insert into #payments values (3,1,222345,6789.09,1208.00,6,2011) insert into #payments values (3,1,222345,1234.09,1208.00,7,2011) ```I need to return 2 different resultsThis one sums the total values of months 1+2 for year 2011 before the month of 3 (first occurrence of month that has a deviation) when the amount exceeds the value in table MaxPremia. I need to display in one row the total for months 1+2 and the values for month 3.IdNo TotalPayments foryear formonth DeviationValue222345 158 2001 3 1912.90222345158200149024.00The second output sums all the months (TotalPayments) that are within the limits ie months 1,2,5 and shows the deviation rows:IdNoTotalPaymentsforyearformonthDeviationValue222345248200131912.90222345248200149024.00222345248200167997.09How can I achieve these results?Thanks

khtan
In (Som, Ni, Yak)

Singapore
17689 Posts

 Posted - 11/13/2013 :  22:49:08 quote:(first occurrence of month that has a deviation)What do you mean by deviation ?quote:when the amount exceeds the value in table MaxPremiaWhat amount ? Please tell us the column name(s)KHTime is always against us

collie
Constraint Violating Yak Guru

400 Posts

 Posted - 11/14/2013 :  00:45:32 By deviation which is probably the wrong term i mean that the total per year/month exceeds the amount in maxPremia table for that year.I sum compensation+providence(23.00 45.0045.00 45.00)for months 1 and 2 as after that month 3 has a total per month/year that exceeds the amount in maxPremia table.I need the total for the months prior to the first month that has the amount that exceeds the amount in maxPremia table.In addition i also need total for months 1,2,5 as these are all the months that their sum per month/year don't exceed the amount in maxPremia table.

khtan
In (Som, Ni, Yak)

Singapore
17689 Posts

 Posted - 11/14/2013 :  01:55:49 Query 1``` select * from #payments p cross apply ( select TotalPayments = sum(Compensation + Providence) from #payments x where x.ForYear = p.ForYear and x.ForMonth <= p.ForMonth ) c inner join #maxPremia m on p.ForYear = m.foryear where c.TotalPayments <= maxPremia```Query 2``` select * from #payments p inner join #maxPremia m on p.ForYear = m.foryear where p.Compensation + p.Providence < maxPremia ```KHTime is always against us

collie
Constraint Violating Yak Guru

400 Posts

 Posted - 11/15/2013 :  10:56:30 Hi,I need to get the following results and i don't get it with the queries. I think i am not making myself clear.ReceiptNo LineNu IdNo Compensation Providence ForMonth ForYearTotalPaymentsforyearmaxPremia1122234523.0045.001201168.0020111910.001222234545.0045.0022011158.0020111910.00What i need is the following:ReceiptNoLineNuIdNoforyearformonth[Compensation]+[Providence])Total rows where[Compensation]+[Providence])< 1910.0021222345201149024.002160.931222345201167997.092160.9I don't calculate month 7 as the max month where [Compensation]+[Providence]>1910 is 6
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC