I am trying to combine two queries into one UNION query. The first sub-query works, but the second creates the following error at the end of the first iif statement: "Syntax error in union query"
Does anybody know why?
The query I was originally using was:
I tried using nested queries with the ultimate aim of moving the queries to VBA only. Is this too ambitious?!
Does anybody know why?
Code:
Select Symbol, MarketValue AS [Value], Type, RiskType FROM [SELECT qryReportPositions.Symbol, Sum(qryReportPositions.MV) as MarketValue, qryReportPositions.Type, "Position" AS RiskType FROM qryReportPositions GROUP BY qryReportPositions.Symbol, qryReportPositions.Type, "Position" HAVING (((qryReportPositions.Type)="Common Stock")) ]. AS tmpVarPositions UNION SELECT Symbol, Sum(MarketValue), AlteredType as Type, RiskType FROM [SELECT IIf([Type]="Currency",qryReportPositions.Symbol,[PriceCurrency]) AS Symbol, "Currency" AS AlteredType, Sum(qryReportPositions.MV) AS MarketValue, IIf([PriceCurrency]<>"USD","Currency","Position") AS RiskType FROM qryReportPositions GROUP BY IIf([Type]="Currency",qryReportPositions.Symbol,[PriceCurrency]), "Currency", IIf([PriceCurrency]<>"USD","Currency","Position"), qryReportPositions.Type, qryReportPositions.PriceCurrency HAVING (((qryReportPositions.Type)="Currency" Or (qryReportPositions.Type)="Fund")) OR (((qryReportPositions.PriceCurrency)<>"USD")) ]. AS tmpVarCurrency GROUP BY Symbol, AlteredType, RiskType ORDER BY Symbol;
Code:
Select Symbol, Value, Type, RiskType FROM qryVARPositions UNION SELECT Symbol, Sum(MarketValue), AlteredType as Type, RiskType FROM qryVARCurrency GROUP BY Symbol, AlteredType, RiskType ORDER BY Symbol;
Comment