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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Groups ,Sum and Totals how to do it ??

Author  Topic 

nvoyatzopoulos
Starting Member

7 Posts

Posted - 2012-11-11 : 02:23:28
Hello , i'm new and i want your help guys! I've 3 tables with one common field (id) .
the data look like this ..
companyid |datetrn |income| expenses | tax income | tax expenses |..
01 |2012/01/01|100 | 0 | 0 | 10 |..
02 |2012/02/15|0 | 150 | 12 | 0 |..
03 |2012/03/20|1500 | 1000 | 0 | 100 |..

so here start the troubles .. I want to join those 3 tables with id (ok with this) and present results like the following ..

inc. sum|exp.sum|tot.period|in.tax sum|ex.tax sum|tot.period
january | 100 | 0 | 100 | 0 | 10 | 10
february | 0 | 150 | 50 | 12 | 0 | -2
march | 1000 | 0 | 1050 | 0 | 100 | 98
........

Is this possible ?

thanks in advance !

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-11 : 07:10:03
Based on your description it doesn't look like you will need to join three tables. Also, although the columns in the first table appear to be self-explanatory, it is not clear to me what the columns in the result set are, or how the data in the first table are used to compute the results. So this may be completely wrong, but the general idea would be this:
select
datename(month,datetrn),
sum(income) as [inc.sum],
sum(income+[tax income]) as [tot.period]
from
YourTable
group by
datename(month,datetrn)
Go to Top of Page
   

- Advertisement -