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 |
prasantapaul
Starting Member
6 Posts |
Posted - 2014-03-13 : 05:52:59
|
I am having many tables in my database from where I want to get various statistical data.Below is the simpler version of what I am doing now. SELECT [ID] ,[Sale] ,[Purchase] FROM ( SELECT A.[ID] ,SUM(B.[Sale]) AS [Sale] ,0 AS [Purchase] FROM A INNER JOIN B ON A.[ID] = B.[ID] WHERE B.[SaleDate] BETWEEN @StartDate AND @EndDate GROUP BY A.[ID] UNION ALL SELECT A.[ID] ,0 AS [Sale] ,SUM(C.[Purchase]) AS [Purchase] FROM A INNER JOIN C ON A.[ID] = C.[ID] WHERE B.[PurchaseDate] BETWEEN @StartDate AND @EndDate GROUP BY A.[ID] ) AS TIs this the correct way to perform this operation? |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2014-03-13 : 08:30:20
|
everything is correct until you stuck into an issue :) but its not the case with design/architecture, which needs to be addressed well before you get into an issue. :) Anyway, what is the problem you're facing with this query? Unless there is an issue I am not sure if we can tell you if the approach is better or not, since we don't know about the structure of the tables, the data placement inside it.However if its possible to achieve the result with one query (without using union all) that might be a better approach.CheersMIK |
|
|
|
|
|