I have 2 tables with data of invoices like date invoice no. supplier etc. First table differs from another that's why they are creates as separated tables. However both table have identical column which I wanted to UNION and GROUP BY.
When I am doing things with one table everything is OK but when trying to UNION I get separated group by data for example if it is grouped by client and country I get two countries (BE and below also BE) of the same client. Result should provide me only one country with summed VATamount.
I read some articles on the Internet however it does not help me with my problem. So far my syntax looks like this:
SELECT [Faktury - od nas].customerKey, [Faktury - od nas].InvoiceCountry, Sum([Faktury - od nas].VATamount) AS SumaOfVATamount
FROM [Faktury - od nas]
GROUP BY [Faktury - od nas].customerKey, [Faktury - od nas].InvoiceCountry
UNION
SELECT [Faktury - od nich].customerKey, [Faktury - od nich].InvoiceCountry, Sum([Faktury - od nich].VATamount) AS SumaOfVATamount
FROM [Faktury - od nich]
GROUP BY [Faktury - od nich].customerKey, [Faktury - od nich].InvoiceCountry
So far output:
01-72 --- BE --- 125,00
01-72 --- BE --- 180,07
11-54 --- DE --- 206,34
11-54 --- DE --- 100,00
23-65 --- FR --- 23,00
23-65 --- FR --- 100,76
Desired output:
01-72 --- BE --- 305,07
11-54 --- DE --- 306,34
23-65 --- FR --- 123,76
Edit: I was doing some changes in query and found out it stoped grouping as supposed to. Can you help?
SELECT [E100 Key], [Nazwa Klienta], [Kraj], [Okres], Sum(Format(Round([Kwota1],2),'Standard')) AS [Kwota], [Waluta], [Status], [Rozliczenie]
FROM(
SELECT [Faktury - od nas].customerKey AS [E100 Key], [Faktury - od nas].CustomerName AS [Nazwa Klienta], [Faktury - od nas].InvoiceCountry AS Kraj, [Faktury - od nas].Okres AS Okres, Sum([Faktury - od nas].VATamount) AS Kwota1, [Faktury - od nas].Currency AS Waluta, [Faktury - od nas].Status AS Status, [Faktury - od nas].[Transtax invoice No] AS Rozliczenie
FROM [Faktury - od nas]
GROUP BY [Faktury - od nas].customerKey, [Faktury - od nas].CustomerName, [Faktury - od nas].InvoiceCountry, [Faktury - od nas].Okres, [Faktury - od nas].Currency, [Faktury - od nas].Status, [Faktury - od nas].[Transtax invoice No]
HAVING ((([Faktury - od nas].Okres) Is Not Null))
UNION
SELECT [Faktury - od nich].customerKey AS [E100 Key], [Faktury - od nich].CustomerName AS [Nazwa Klienta], [Faktury - od nich].InvoiceCountry AS Kraj, [Faktury - od nich].Okres AS Okres, Sum([Faktury - od nich].VATamount) AS Kwota1, [Faktury - od nich].Currency AS Waluta, [Faktury - od nich].Status AS Status, [Faktury - od nich].[Transtax invoice No] AS Rozliczenie
FROM [Faktury - od nich]
GROUP BY [Faktury - od nich].customerKey, [Faktury - od nich].CustomerName, [Faktury - od nich].InvoiceCountry, [Faktury - od nich].Okres, [Faktury - od nich].Currency, [Faktury - od nich].Status, [Faktury - od nich].[Transtax invoice No]
HAVING ((([Faktury - od nich].Okres) Is Not Null))
)GROUP BY [E100 Key], [Nazwa Klienta], [Kraj], [Okres], [Waluta], [Status], [Rozliczenie];