473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem Combining Queries

119 100+
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?

Expand|Select|Wrap|Line Numbers
  1. Select Symbol, MarketValue AS [Value], Type, RiskType FROM
  2. [SELECT qryReportPositions.Symbol, Sum(qryReportPositions.MV) as MarketValue, qryReportPositions.Type, "Position" AS RiskType
  3. FROM qryReportPositions
  4. GROUP BY qryReportPositions.Symbol, qryReportPositions.Type, "Position"
  5. HAVING (((qryReportPositions.Type)="Common Stock"))
  6. ]. AS tmpVarPositions
  7.  
  8. UNION 
  9. SELECT Symbol, Sum(MarketValue), AlteredType as Type, RiskType
  10. FROM 
  11.  
  12. [SELECT IIf([Type]="Currency",qryReportPositions.Symbol,[PriceCurrency]) AS Symbol, "Currency" AS AlteredType, Sum(qryReportPositions.MV) AS MarketValue, IIf([PriceCurrency]<>"USD","Currency","Position") AS RiskType
  13. FROM qryReportPositions
  14. GROUP BY IIf([Type]="Currency",qryReportPositions.Symbol,[PriceCurrency]), "Currency", IIf([PriceCurrency]<>"USD","Currency","Position"), qryReportPositions.Type, qryReportPositions.PriceCurrency
  15. HAVING (((qryReportPositions.Type)="Currency" Or (qryReportPositions.Type)="Fund")) OR (((qryReportPositions.PriceCurrency)<>"USD"))
  16. ]. AS tmpVarCurrency
  17. GROUP BY Symbol, AlteredType, RiskType
  18. ORDER BY Symbol;
  19.  
The query I was originally using was:

Expand|Select|Wrap|Line Numbers
  1. Select  Symbol, Value, Type, RiskType 
  2. FROM qryVARPositions
  3. UNION SELECT Symbol, Sum(MarketValue), AlteredType as Type, RiskType
  4. FROM qryVARCurrency
  5. GROUP BY Symbol, AlteredType, RiskType
  6. ORDER BY Symbol;
  7.  
I tried using nested queries with the ultimate aim of moving the queries to VBA only. Is this too ambitious?!
May 28 '08 #1
3 1528
NeoPa
32,556 Expert Mod 16PB
I think you may be suffering from a problem described in Access QueryDefs Mis-save Subquery SQL.
May 29 '08 #2
billelev
119 100+
I think it is along those lines. I replaced references to fields in the form "[FieldName]" to "tblName.FieldName" and it worked after that. So definitely related to the use of square brackets. The following code works:

Expand|Select|Wrap|Line Numbers
  1. Select Symbol, MarketValue AS [Value], Type, RiskType FROM
  2. [SELECT qryReportPositions.Symbol, Sum(qryReportPositions.MV) as MarketValue, qryReportPositions.Type, "Position" AS RiskType
  3. FROM qryReportPositions
  4. GROUP BY qryReportPositions.Symbol, qryReportPositions.Type, "Position"
  5. HAVING (((qryReportPositions.Type)="Common Stock"))
  6. ]. AS tmpVarPositions
  7.  
  8. UNION
  9.  
  10. SELECT Symbol, Sum(MarketValue), AlteredType as Type, RiskType
  11. FROM
  12. [SELECT IIf(qryReportPositions.Type="Currency",qryReportPositions.Symbol,qryReportPositions.PriceCurrency) AS Symbol, "Currency" AS AlteredType, Sum(qryReportPositions.MV) AS MarketValue, IIf(qryReportPositions.PriceCurrency<>"USD","Currency","Position") AS RiskType
  13. FROM qryReportPositions
  14. GROUP BY IIf(qryReportPositions.Type="Currency",qryReportPositions.Symbol,qryReportPositions.PriceCurrency), "Currency", IIf(qryReportPositions.PriceCurrency<>"USD","Currency","Position"), qryReportPositions.Type, qryReportPositions.PriceCurrency
  15. HAVING (((qryReportPositions.Type)="Currency" Or (qryReportPositions.Type)="Fund")) OR (((qryReportPositions.PriceCurrency)<>"USD"))
  16. ]. AS tmpVarCurrency
  17.  
  18. GROUP BY Symbol, AlteredType, RiskType
  19. ORDER BY Symbol;
May 30 '08 #3
NeoPa
32,556 Expert Mod 16PB
LOL.

That's not what the article is about - but who cares if it enabled you to find and fix your problem :D

Am I right in thinking you use Access 2003 or later? Those versions are more forgiving of the bug that Access introduces into your SQL. It is present in yours, but obviously not added by you, but by Access itself. The article goes into greater detail if you're interested, but I'm pleased to see that you resolved your problem anyway.

PS. That also means that what I thought was your problem wasn't. You've got to love irony huh?
Jun 2 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: frizzle | last post by:
Hi, I know this might sound strange but i think(/hope) it's quite simple: I'm running 2 queries in a mysql DB, first one returns 20 results. Now how can i echo results from the second query...
2
by: SomeDude | last post by:
Lo group, I am wondering if there is a way of combining two SELECT statements into a single query. Here's the obligatory example to clarify things: SELECT id WHERE name=mike SELECT bills...
1
by: Bruce MacDonald | last post by:
I've got a question/request for the SQL gurus. I'm building a model of bandwidth demand in MS Access and want to get aggregated results for demand at each PCP in each time period. The two...
2
by: BeruthialsCat | last post by:
I have 4 crosstabs which are keyed on a managerID field. I want to combine them to provide the data for a graph. I currently have them combined in a union query to produce 4 rows of data for each...
1
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured...
2
by: billelev | last post by:
Does anyone know if it is possible to combine two queries that have the same fields? I basically want to combine a number of fields with the totals for those fields into one query (see example...
7
by: tcveltma | last post by:
Hi again, Ok, so I have about 15 crosstab queries. Each crosstab query is an employee's name. Within the query are the weeks as the column heading, the work order numbers as the row heading, and...
19
by: Gilberto | last post by:
Hello I have created TWO different queries (for products belonging to FRONT and REAR) which use product information to filter the total (sum all belonging to the same category) of all the FRONT...
7
by: MNNovice | last post by:
I have 3 queries that show expenses on various projects/funds. I would like to combine the results and show various expenses on each project/fund etc. But when I try to combine these 3 queries,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.