Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining Results in a Union Query?

Status
Not open for further replies.

supportservice

Technical User
Mar 12, 2012
63
Hello,
I have this UNION query but need to have the totals summed when the MainAccountDesc is the same.

Code:
 SELECT RotocoSummary.AccountCategoryDesc, RotocoSummary.MainAccountDesc, RotocoSummary.MTD, RotocoSummary.LMTD, RotocoSummary.YTD, RotocoSummary.LYTD
FROM RotocoSummary LEFT JOIN SB1Summary ON (RotocoSummary.MainAccountDesc = SB1Summary.MainAccountDesc) AND (RotocoSummary.AccountCategoryDesc = SB1Summary.AccountCategoryDesc)

UNION SELECT SB1Summary.AccountCategoryDesc, SB1Summary.MainAccountDesc, SB1Summary.MTD, SB1Summary.LMTD, SB1Summary.YTD, SB1Summary.LYTD
FROM RotocoSummary LEFT JOIN SB1Summary ON (RotocoSummary.MainAccountDesc = SB1Summary.MainAccountDesc) AND (RotocoSummary.AccountCategoryDesc = SB1Summary.AccountCategoryDesc);

RESULT:
Code:
 AccountCategoryDesc	MainAccountDesc	MTD	LMTD	YTD	LYTD
Assets	ACCUMULATED DEPRECIATION	0	3316.17	10149.61	13264.68
Assets	ACCUMULATED DEPRECIATION	0	21615.27	23158	86461.08

It should be combined not two separate lines.
I'm not sure how to do that?
 
Perhaps this ?
Code:
SELECT AccountCategoryDesc,MainAccountDesc,Sum(MTD) AS totMTD,Sum(LMTD) AS totLMTD,Sum(YTD) AS totYTD,Sum(LYTD) AS sLYTD
FROM (SELECT AccountCategoryDesc,MainAccountDesc,MTD,LMTD,YTD,LYTD FROM RotocoSummary
UNION ALL
SELECT AccountCategoryDesc,MainAccountDesc,MTD,LMTD,YTD,LYTD FROM SB1Summary) U
GROUP BY AccountCategoryDesc,MainAccountDesc

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top