| Author |
Topic |
|
kblurry
Starting Member
6 Posts |
Posted - 2011-12-10 : 11:22:51
|
i have two tables:table 1:AmountPostDatetable 2:CostRevenueClockDateRevenue = RevenueTotalCost = Amount + CostProfit = Revenue - TotalCosti have to sum the two tables according to month and year so it would i guess look like:month | TotalCost | Revenue | Profit | Year | TotalCost | Revenue | Yr1 ------------------------------------2010*-----------------------20112 ------------------------------------2010*-----------------------20113 ------------------------------------2010*-----------------------20114 ------------------------------------2010*-----------------------20115 ------------------------------------2010*-----------------------20116 ------------------------------------2010*-----------------------20117 ------------------------------------2010*-----------------------20118 ------------------------------------2010*-----------------------20119 ------------------------------------2010*-----------------------201110 ------------------------------------2010*----------------------201111 ------------------------------------2010*----------------------201112------------------------------------2010*-----------------------2011*must be the year that is in the database(dynamic year) up to current year (2011)i have this query to start:SELECT *FROM ( SELECT TOP 100 2003 + ROW_NUMBER() OVER (ORDER BY PostingDate) AS Yr, SUM(Amount) as Total FROM table1 GROUP BY PostingDate ) YearsWHERE Yr <= YEAR(GETDATE()) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-10 : 11:35:13
|
| you sample output doesnt make much sense. what would be value of other columns? also whats the column based on which you need to determine the year?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
kblurry
Starting Member
6 Posts |
Posted - 2011-12-10 : 11:46:05
|
This is what the empty columns value will be:Revenue = RevenueTotalCost = Amount + CostProfit = Revenue - TotalCost i need all the years in the database, so if i have 2009 as the least year in the database, i need the Revenue, TotalCost,Profit for that year. like: for 2009 the cost,revenue,profit would be listed by month |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-10 : 12:12:23
|
| you dont have year column in database. that's why i asked which column to be considered for taking year values (PostDate or ClockDate)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
kblurry
Starting Member
6 Posts |
Posted - 2011-12-10 : 12:58:51
|
The year date is supposed to be the new column that combines the year PostingDate of table 1 and ClockDate of table 2. something like: SELECT [Month], SUM(Cost) AS TotalCost, SUM(Revenue) AS Revenue, SUM(Revenue - Cost) AS ProfitFROM ( SELECT MONTH([PostingDate]) AS [Month], SUM(Amount) AS Cost, 0 AS Revenue FROM table1 where YEAR([PostingDate]) = '2010' GROUP BY MONTH([PostingDate]) UNION ALL SELECT MONTH([ClockDate]), SUM(Cost), SUM(Revenue) FROM table2 where YEAR([ClockDate]) = '2010' GROUP BY MONTH([ClockDate])) tmpGROUP BY [Month]ORDER BY [Month] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-11 : 01:09:30
|
| ok...so now whats the issue you're facing with posted code above?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
kblurry
Starting Member
6 Posts |
Posted - 2011-12-11 : 16:49:28
|
| the issue is I need the year to be dynamic, the where year = '2010' should not be fixed. It needs to figure out the year in my database without me specifying it to be 2009,2010 etc. The where clause should be where year = //query the database for all years up to current year which is (getdate) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-11 : 23:40:56
|
you mean this?SELECT [Year],[Month], SUM(Cost) AS TotalCost, SUM(Revenue) AS Revenue, SUM(Revenue - Cost) AS ProfitFROM ( SELECT YEAR([PostingDate]) AS [Year],MONTH([PostingDate]) AS [Month], SUM(Amount) AS Cost, 0 AS Revenue FROM table1 GROUP BY YEAR([PostingDate]),MONTH([PostingDate]) UNION ALL SELECT YEAR([ClockDate]) ,MONTH([ClockDate]), SUM(Cost), SUM(Revenue) FROM table2 GROUP BY YEAR([ClockDate]),MONTH([ClockDate])) tmpGROUP BY [Year],[Month]ORDER BY [Year],[Month] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
kblurry
Starting Member
6 Posts |
Posted - 2011-12-12 : 10:42:22
|
| Thanks man, so simple i missed. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-12 : 10:46:02
|
no problemyou're welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|