Union Query with group and Sum (1 Viewer)

Tskutnik

Registered User.
Local time
Today, 11:27
Joined
Sep 15, 2012
Messages
229
Hi all. Hope you are all doing well.

Easy one...

In the Union query below (below the XX's) I need to
Group by:
AsOf,
AcctKey,
SecKey,
PositionRef

and
Sum By:
Quantity_Impact,
MarketVal_Impact,
Cash_Impact,

Thanks

XXXXXXXXXXXXX

SELECT
AsOf,
AcctKey,
SecKey,
Quantity_Impact,
MarketVal_Impact,
Cash_Impact,
PositionRef
FROM Activity_Cash_1;

UNION ALL SELECT
AsOf,
AcctKey,
SecKey,
Quantity_Impact,
MarketVal_Impact,
Cash_Impact,
PositionRef
FROM Activity_Security_1;
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:27
Joined
Oct 29, 2018
Messages
21,456
Hi. You could try:
SQL:
SELECT AsOf, AcctKey, SecKey, PositionRef,
Sum(Quantity_Impact), Sum(MarketVal_Impact), Sum(Cash_Impact)
FROM (Your Union Query Here)
GROUP BY AsOf, AcctKey, SecKey, PositionRef
Hope that helps...
 

CJ_London

Super Moderator
Staff member
Local time
Today, 16:27
Joined
Feb 19, 2013
Messages
16,607
in the sql window modify your union query to alias it

SELECT * FROM (union query) AS U

then return to the query builder and create your aggregate query

Edit: DBG has provided the detailed solution:)
 

Tskutnik

Registered User.
Local time
Today, 11:27
Joined
Sep 15, 2012
Messages
229
Thanks all- perfectly simple.... I was a victim of syntax errors.... aren't we all.
 

Tskutnik

Registered User.
Local time
Today, 11:27
Joined
Sep 15, 2012
Messages
229
CJ - I did not try your suggestion because I was not clear what you meant (newbie). If that why Access is inserting the "[%$##@_Alias]" text? How exactly do I fix this?


SELECT [%$##@_Alias].AsOf, [%$##@_Alias].AcctKey, [%$##@_Alias].SecKey, [%$##@_Alias].PositionRef, Sum([%$##@_Alias].Quantity_Impact) AS Sum_Quantity_Impact, Sum([%$##@_Alias].MarketVal_Impact) AS Sum_MarketVal_Impact, Sum([%$##@_Alias].Cash_Impact) AS Sum_Cash_Impact
FROM (SELECT
AsOf,
AcctKey,
SecKey,
Quantity_Impact,
MarketVal_Impact,
Cash_Impact,
PositionRef
FROM Activity_Cash_1


UNION ALL SELECT
AsOf,
AcctKey,
SecKey,
Quantity_Impact,
MarketVal_Impact,
Cash_Impact,
PositionRef
FROM Activity_Security_1

) AS [%$##@_Alias]
GROUP BY [%$##@_Alias].AsOf, [%$##@_Alias].AcctKey, [%$##@_Alias].SecKey, [%$##@_Alias].PositionRef;
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:27
Joined
Oct 29, 2018
Messages
21,456
CJ - I did not try your suggestion because I was not clear what you meant (newbie). If that why Access is inserting the "[%$##@_Alias]" text? How exactly do I fix this?


SELECT [%$##@_Alias].AsOf, [%$##@_Alias].AcctKey, [%$##@_Alias].SecKey, [%$##@_Alias].PositionRef, Sum([%$##@_Alias].Quantity_Impact) AS Sum_Quantity_Impact, Sum([%$##@_Alias].MarketVal_Impact) AS Sum_MarketVal_Impact, Sum([%$##@_Alias].Cash_Impact) AS Sum_Cash_Impact
FROM (SELECT
AsOf,
AcctKey,
SecKey,
Quantity_Impact,
MarketVal_Impact,
Cash_Impact,
PositionRef
FROM Activity_Cash_1


UNION ALL SELECT
AsOf,
AcctKey,
SecKey,
Quantity_Impact,
MarketVal_Impact,
Cash_Impact,
PositionRef
FROM Activity_Security_1

) AS [%$##@_Alias]
GROUP BY [%$##@_Alias].AsOf, [%$##@_Alias].AcctKey, [%$##@_Alias].SecKey, [%$##@_Alias].PositionRef;
Hi. You can use any alias you like. Try replacing everything Access added for you with something like [MyUnion].
 

Users who are viewing this thread

Top Bottom