473,769 Members | 7,315 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem Combining Queries

119 New Member
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 1555
NeoPa
32,573 Recognized Expert Moderator MVP
I think you may be suffering from a problem described in Access QueryDefs Mis-save Subquery SQL.
May 29 '08 #2
billelev
119 New Member
I think it is along those lines. I replaced references to fields in the form "[FieldName]" to "tblName.FieldN ame" 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,573 Recognized Expert Moderator MVP
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
2797
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 in the 1st query's result, like:
2
2111
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 WHERE nameid=12 (12 being the result of the first query) Right now I am executing two separate calls and I would ofcourse like to
1
6013
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 queries below are used to count the number of households of each Socio-Economic Type (each postcode has been allocated an SE-Type), and use that to count the number of connections requiring a bandwidth less than 512 that will be required at the PCP in...
2
4162
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 manager by month. What i think i need is to change the orientation so i have a column heading that represenst each crosstab, a manager ID column and a month column. So far ive thought of making a temp table using make table on one of the...
1
2068
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 query language, which forces me to do the join manually via arrays. Right now, I'm having trouble getting these query results (which are in
2
1479
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 below). eg. If the original query contained four items: a, b, c and d, the resulting query or table combination would be: Name Amount a 1 b 2 c 3 d 4 ALL 10
7
2754
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 the hours as a value. Each query comes from a different employee table (1 table per employee). Is there any way I can combine all the queries into 1 query/report? So far it seems to me that the only way I'll have a chance at making every...
19
1839
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 products belonging to every category. Same thing applies to the second query but just with products belonging to the REAR. The type of categories are the same for both queries (RED, BLUE, BLACK) and i just change the "CRITERIA" under the SEATING...
7
2743
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, the data gets repeated multiple times and the result becomes huge. Query 1: qryAPDetail has 92 records Query 2: qryPayDetail has 77 records Query 3: qryAllocDetail has 26 records How do I combine these queries to get a combined result...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.