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.
Author |
Topic |
Ryerocks
Starting Member
16 Posts |
Posted - 2012-03-22 : 10:51:11
|
A few people were kind enough to help me build a query a number of weeks ago, but it has become a bit more complex than before.Here is the solution query to my previous issue:.............SELECT ContractYear, SalesAmt, ExpenseAmt, SalesAmt - ExpenseAmt as YearSurplus, (SELECT SUM(SalesAmt - ExpenseAmt) FROM dbo.Sales WHERE ContractYear <= s.ContractYear) as RunningSurplusFROM dbo.Sales s...............This calculated a running surplus that was always based on the previous year's surplus and so on. Now, its possible that any ContractYear could also have a forcedSurplus amount. When this is the case, I need to ignore all the prior surplus values earlier than that contract year, and just use the forcedsurplus year as the starting year, and then using the same calculation as before to calculate the running surplus each year.I hope that makes sense. I found it somewhat hard to describe. |
|
Ryerocks
Starting Member
16 Posts |
Posted - 2012-03-22 : 14:30:16
|
To describe what I need a bit more..For each row, I need to determine what is the most recent prior contract year that has a forcedsurplus amount. Then I need to use that contract year as the base year for the subquery above, and add in the forcedsurplus amount. |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-03-22 : 14:46:50
|
Sample data? Expected output? If you supply that in a consumable format you'll probably get better help faster.http://www.sqlservercentral.com/articles/Best+Practices/61537/http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
Ryerocks
Starting Member
16 Posts |
Posted - 2012-03-22 : 15:45:11
|
Thanks for the advice. I knew I didn't draw up my question/details very well.I think I may have solved my own problem though.. just started throwing together small pieces that worked and then re-using them in case statements..SELECT TOP (100) PERCENT ContractYear, Sales, Expenses, ForcedSurplus, Sales - Expenses AS YearSurplus, CASE WHEN (SELECT MAX(ContractYear) AS Expr1 FROM dbo.TestSales WHERE (ContractYear <= s.ContractYear) AND (ContractID = s.ContractID) AND (ForcedSurplus IS NOT NULL)) IS NOT NULL THEN (SELECT SUM(Sales - Expenses + ISNULL(ForcedSurplus, 0)) AS Expr1 FROM dbo.TestSales AS TestSales_1 WHERE (ContractYear <= s.ContractYear) AND (ContractID = s.ContractID) AND (ContractYear >= (SELECT MAX(ContractYear) AS Expr1 FROM dbo.TestSales AS TestSales_2 WHERE (ContractYear <= s.ContractYear) AND (ContractID = s.ContractID) AND (ForcedSurplus IS NOT NULL)))) ELSE (SELECT SUM(Sales - Expenses) AS Expr1 FROM dbo.TestSales WHERE (ContractYear <= s.ContractYear) AND (ContractID = s.ContractID)) END AS RunningSurplus, (SELECT MAX(ContractYear) AS Expr1 FROM dbo.TestSales WHERE (ContractYear <= s.ContractYear) AND (ContractID = s.ContractID) AND (ForcedSurplus IS NOT NULL)) AS ForcedYear, ContractIDFROM dbo.TestSales AS sORDER BY ContractID, ContractYear |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-22 : 15:56:40
|
why do you need all these subqueries?i think better to do like thisSELECT s.*,s1.RunningSurplus,s1.ContractYear AS ForcedYearFROM dbo.TestSales AS sCROSS APPLY (SELECT ContractYear ,SUM(Sales - Expenses + ISNULL(ForcedSurplus, 0)) AS RunningSurplus, ROW_NUMBER() OVER (ORDER BY ContractYear DESC) AS Seq FROM dbo.TestSales WHERE ContractYear <= s.ContractYear AND (ContractID = s.ContractID) AND (ForcedSurplus IS NOT NULL) GROUP BY ContractYear )s1WHERE s1.Seq=1ORDER BY ContractID, ContractYear ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Ryerocks
Starting Member
16 Posts |
Posted - 2012-03-23 : 10:08:54
|
ha.. I did all those subqueries, because I don't know what I'm doing, and I'm just thrilled I was able to write something like that and get the desired result.I'll try yours and see if it yields the same dataset... |
 |
|
Ryerocks
Starting Member
16 Posts |
Posted - 2012-03-23 : 10:13:32
|
That doesn't spit out what mine does. Only 3 rows were returned. The result dataset should have the same number of rows as the the initial table has. (currently, 8)TestSales table:Columns: SalesID, Sales, Expenses, ContractYear, ContractID, ForcedSurplus1 3.0000 1.0000 2001 1 NULL2 5.0000 4.0000 2002 1 73 2.0000 1.0000 2003 1 NULL4 6.0000 3.0000 2001 2 NULL5 8.0000 3.0000 2002 2 NULL6 9.0000 6.0000 2003 2 NULL7 4.0000 1.0000 2004 1 68 5.0000 4.0000 2004 2 NULLmy query's result dataset:Columns: ContractYear, Sales, Expenses, ForcedSurplus, YearSurplus, RunningSurplus, ForcedYear, ContractID2001 3.0000 1.0000 NULL 2.0000 2.0000 NULL 12002 5.0000 4.0000 7 1.0000 8.0000 2002 12003 2.0000 1.0000 NULL 1.0000 9.0000 2002 12004 4.0000 1.0000 6 3.0000 9.0000 2004 12001 6.0000 3.0000 NULL 3.0000 3.0000 NULL 22002 8.0000 3.0000 NULL 5.0000 8.0000 NULL 22003 9.0000 6.0000 NULL 3.0000 11.0000 NULL 22004 5.0000 4.0000 NULL 1.0000 12.0000 NULL 2darn, how do I get the column names to show up when I copy and paste a dataset like that? |
 |
|
|
|
|
|
|