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
 How to get data from 2 tables in that way i need

Author  Topic 

ksemaska
Starting Member

1 Post

Posted - 2013-04-23 : 13:29:13
Hello proffesionals!

This what i got like 2 tables(1 is money income 2nd is money outcome) almost with same column names like

id name user_id catalogue sum date(timestamp)

This what i need:
i need get data from both tables, then count SUM(sum) from both as one, but this is a bit hard because 2nd table sum values should be negative then SUM is counted. Also each SUM value should be counted for each day where we have any income,outcome (or just one of these) something like GROUP BY CAST(`data`AS DATE). Any ideas? Thanks for replies! :)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-23 : 13:43:08
[code]
select dateadd(dd,datediff(dd,0,[date]),0), SUM([sum]) AS Total
FROM
(
SELECT *
FROM table1
UNION ALL
SELECT *
FROM table2
)t
GROUP BY dateadd(dd,datediff(dd,0,[date]),0)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -