SOLVED

Sum by Year in addition to by Month

%3CLINGO-SUB%20id%3D%22lingo-sub-3179252%22%20slang%3D%22en-US%22%3ESum%20by%20Year%20in%20addition%20to%20by%20Month%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3179252%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20developed%20the%20following%20query%20that%20sums%20by%20month.%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20to%20add%20a%20sum%20by%20year%20too.%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20to%20best%20do%20this%3F%26nbsp%3B%3C%2FP%3E%3CP%3Ethank%20you%20in%20advance...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESELECT%20Format(%5BWithdrawalDate%5D%2C'mmmm%20yyyy')%20AS%20%5BMonth%5D%2C%20Sum(tblDraws_CAWC.LocalAcctNoLR)%20AS%20SumOfLocalAcctNoLR%2C%20Sum(tblDraws_CAWC.LiquidityRes)%20AS%20SumOfLiquidityRes%2C%20Sum(tblDraws_CAWC.TLocalAcctCalc)%20AS%20SumOfTLocalAcctCalc%3CBR%20%2F%3EFROM%20tblDraws_CAWC%3CBR%20%2F%3EGROUP%20BY%20Format(%5BWithdrawalDate%5D%2C'mmmm%20yyyy')%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3179252%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eaccess%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3179334%22%20slang%3D%22en-US%22%3ERe%3A%20Sum%20by%20Year%20in%20addition%20to%20by%20Month%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3179334%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1029845%22%20target%3D%22_blank%22%3E%40Tony2021%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20use%20a%20UNION%20query%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3ESELECT%20%22Monthly%22%20AS%20%5BDate%20Period%5D%2C%20Format(%5BWithdrawalDate%5D%2C'mmmm%20yyyy')%20AS%20%5BMonth%5D%2C%20Sum(tblDraws_CAWC.LocalAcctNoLR)%20AS%20SumOfLocalAcctNoLR%2C%20Sum(tblDraws_CAWC.LiquidityRes)%20AS%20SumOfLiquidityRes%2C%20Sum(tblDraws_CAWC.TLocalAcctCalc)%20AS%20SumOfTLocalAcctCalc%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EFROM%20tblDraws_CAWC%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EGROUP%20BY%20Format(%5BWithdrawalDate%5D%2C'mmmm%20yyyy')%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EUNION%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3ESELECT%20%22Yearly%22%20AS%20%5BDate%20Period%5D%2C%20Format(%5BWithdrawalDate%5D%2C'yyyy')%20AS%20%5BYear%5D%2C%20Sum(tblDraws_CAWC.LocalAcctNoLR)%20AS%20SumOfLocalAcctNoLR%2C%20Sum(tblDraws_CAWC.LiquidityRes)%20AS%20SumOfLiquidityRes%2C%20Sum(tblDraws_CAWC.TLocalAcctCalc)%20AS%20SumOfTLocalAcctCalc%3CBR%20%2F%3EFROM%20tblDraws_CAWC%3CBR%20%2F%3EGROUP%20BY%20Format(%5BWithdrawalDate%5D%2C'yyyy')%3B%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3181327%22%20slang%3D%22en-US%22%3ERe%3A%20Sum%20by%20Year%20in%20addition%20to%20by%20Month%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3181327%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F126074%22%20target%3D%22_blank%22%3E%40George%20Hepworth%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENice.%3CBR%20%2F%3EI%20do%20get%20a%20msg%20box%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Tony2021_0-1645132475075.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F349244iE60B993D92A15A90%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Tony2021_0-1645132475075.png%22%20alt%3D%22Tony2021_0-1645132475075.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ebut%20I%20dont%20even%20see%20qryFacDrawsCAWC%20in%20the%20record%20source%3F%26nbsp%3B%3C%2FP%3E%3CP%3ECould%20a%20sort%20on%20the%20months%20be%20added%3F%20I%20think%20that%20since%20its%20a%20Union%20I%20lose%20the%20query%20design%20window.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Super Contributor

Hello, 

 

I have developed the following query that sums by month. 

I want to add a sum by year too. 

How to best do this? 

thank you in advance...

 

SELECT Format([WithdrawalDate],'mmmm yyyy') AS [Month], Sum(tblDraws_CAWC.LocalAcctNoLR) AS SumOfLocalAcctNoLR, Sum(tblDraws_CAWC.LiquidityRes) AS SumOfLiquidityRes, Sum(tblDraws_CAWC.TLocalAcctCalc) AS SumOfTLocalAcctCalc
FROM tblDraws_CAWC
GROUP BY Format([WithdrawalDate],'mmmm yyyy');

6 Replies

@Tony2021 

 

You can use a UNION query:

SELECT "Monthly" AS [Date Period], Format([WithdrawalDate],'mmmm yyyy') AS [Month], Sum(tblDraws_CAWC.LocalAcctNoLR) AS SumOfLocalAcctNoLR, Sum(tblDraws_CAWC.LiquidityRes) AS SumOfLiquidityRes, Sum(tblDraws_CAWC.TLocalAcctCalc) AS SumOfTLocalAcctCalc
FROM tblDraws_CAWC
GROUP BY Format([WithdrawalDate],'mmmm yyyy')

 

UNION 

SELECT "Yearly" AS [Date Period], Format([WithdrawalDate],'yyyy') AS [Year], Sum(tblDraws_CAWC.LocalAcctNoLR) AS SumOfLocalAcctNoLR, Sum(tblDraws_CAWC.LiquidityRes) AS SumOfLiquidityRes, Sum(tblDraws_CAWC.TLocalAcctCalc) AS SumOfTLocalAcctCalc
FROM tblDraws_CAWC
GROUP BY Format([WithdrawalDate],'yyyy');

@George Hepworth 

Nice.
I do get a msg box:

Tony2021_0-1645132475075.png

 

but I dont even see qryFacDrawsCAWC in the record source? 

Could a sort on the months be added? I think that since its a Union I lose the query design window.  

@Tony2021 

 

Correct Union queries can only be edited in SQL view.

At first glance I'm not sure why the parameter request is being displayed.

 

Did you write your SQL exactly the same way I illustrated it? And is qryFacDrawsCAWC the name of the Union query itself?

 

 

best response confirmed by Tony2021 (Super Contributor)
Solution

@George Hepworth 

Maybe it needs to be:

 

SELECT "Monthly" AS [Date Period], Format([WithdrawalDate],'mmmm yyyy') AS [TimePeriod], Sum(tblDraws_CAWC.LocalAcctNoLR) AS SumOfLocalAcctNoLR, Sum(tblDraws_CAWC.LiquidityRes) AS SumOfLiquidityRes, Sum(tblDraws_CAWC.TLocalAcctCalc) AS SumOfTLocalAcctCalc
FROM tblDraws_CAWC
GROUP BY Format([WithdrawalDate],'mmmm yyyy')

 

UNION 

SELECT "Yearly" AS [Date Period], Format([WithdrawalDate],'yyyy') AS [TimePeriod], Sum(tblDraws_CAWC.LocalAcctNoLR) AS SumOfLocalAcctNoLR, Sum(tblDraws_CAWC.LiquidityRes) AS SumOfLiquidityRes, Sum(tblDraws_CAWC.TLocalAcctCalc) AS SumOfTLocalAcctCalc
FROM tblDraws_CAWC
GROUP BY Format([WithdrawalDate],'yyyy');

 

You'll also want to add an Order by clause to sort the records as needed.

I think I had some kind of corruption. I separated out the queries and ran them separately and I didnt get that parameter msg box. I then combined them into the union query and it still gave me the parameter msg box. I had to delete it then re-create it. Not sure if coudl have been that it was a non union query initially. Anyways, its working. thank you very much!
Microsoft Access is like that sometimes. :)