473,513 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Building query with sub-queries

12 New Member
Hi,

I'm creating a query (queries more like) to build data for upload into a SAP system. For a given material I require a row of data defined, and for the same material another row with one field changed, repeat...

Normally I would just create a query, copy it, make change, save as and then create a final query utilising the UNION function. While this may appear a poor way of defining my queries, it works & I'm generally happy with performance.

However, I have been given a new requirement which requires me to do this several times & therefore my UNION query would end up with 10 queries within... there must be a better way!

From my readings, I believe that creating sub-queries are the way to go... can anyone give me a 101 on sub-query creation (if this is in fact the best way to proceed)?

Sample of final data:
Matl Plant SLoc ...
0001 AB11 AB01 ...
0001 AB11 AB02 ...
0001 AB11 AB03 ...
0002 AB11 AB01 ...
0002 AB11 AB02 ...
0002 AB11 AB03 ...

The SLoc data is defined where if a Matl exists in Plant AB11, then create SLoc AB01, AB02, AB03 and so on.

Here is an example
Expand|Select|Wrap|Line Numbers
  1. SELECT [Status 4 Materials - HIBE NZ02].MaterialUL, [Status 4 Materials - HIBE NZ02].Plant, '0201' AS SLoc
  2. FROM [Status 4 Materials - HIBE NZ02];
...
Sep 11 '07 #1
2 1505
StevoNZ
12 New Member
Actually, I just worked this out myself...
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM
  2. (SELECT [Status 4 Materials - HIBE NZ02].MaterialUL, [Status 4 Materials - HIBE NZ02].Plant, '0201' AS SLoc
  3. FROM [Status 4 Materials - HIBE NZ02])
  4. UNION
  5. (SELECT [Status 4 Materials - HIBE NZ02].MaterialUL, [Status 4 Materials - HIBE NZ02].Plant, '0202' AS SLoc
  6. FROM [Status 4 Materials - HIBE NZ02]);
But the real pain is when I then need to add other conditions, this doesn't work...
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM
  2. (SELECT [Status 4 Materials - HIBE NZ02].MESC, [Status 4 Materials - HIBE NZ02].MaterialUL, [Status 4 Materials - HIBE NZ02].Plant, SLoc: 0201', IIf(IsNull([MRP_ND].[PARTNO]),Null,'001') AS Picking_area
  3. FROM [Status 4 Materials - HIBE NZ02] LEFT JOIN MRP_ND ON [Status 4 Materials - HIBE NZ02].MESC = MRP_ND.PARTNO)
  4. UNION
  5. (SELECT [Status 4 Materials - HIBE NZ02].MESC, [Status 4 Materials - HIBE NZ02].MaterialUL, [Status 4 Materials - HIBE NZ02].Plant, SLoc: 0202', IIf(IsNull([MRP_ND].[PARTNO]),Null,'001') AS Picking_area
  6. FROM [Status 4 Materials - HIBE NZ02] LEFT JOIN MRP_ND ON [Status 4 Materials - HIBE NZ02].MESC = MRP_ND.PARTNO)
  7. UNION
  8. (SELECT [Status 4 Materials - HIBE NZ02].MESC, [Status 4 Materials - HIBE NZ02].MaterialUL, [Status 4 Materials - HIBE NZ02].Plant, SLoc: 0203', IIf(IsNull([MRP_ND].[PARTNO]),Null,'001') AS Picking_area
  9. FROM [Status 4 Materials - HIBE NZ02] LEFT JOIN MRP_ND ON [Status 4 Materials - HIBE NZ02].MESC = MRP_ND.PARTNO);
And I still need to work through other plants: [Status 4 Materials - HIBE NZ03].MESC, [Status 4 Materials - HIBE NZ04].MESC...
Sep 11 '07 #2
MMcCarthy
14,534 Recognized Expert Moderator MVP
OK, I'm a little confused. Can we go back to the logic of what you are trying to do.

What is the structure of your table?

Here is an example of how to post table MetaData :
Table Name=tblBookings
Expand|Select|Wrap|Line Numbers
  1. Field; Type; IndexInfo
  2. StudentID; Autonumber; PK
  3. Family; String; FK
  4. Name; String
  5. University; String; FK
  6. Mark; Numeric
  7. LastAttendance; Date/Time
Next can you explain again exactly what you want to achieve with your query from a logic point of view.
Sep 13 '07 #3

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

Similar topics

18
12784
by: Jeremy Weiss | last post by:
I'm trying to build a database that will handle the monthly billing needs of a small company. I'm charting everything out and here's what I see: table for customers sub table to track payments...
4
2590
by: DebbieG | last post by:
I have a form based on this query: SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr, OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, & ", " & & " " & AS Name,...
4
2849
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
0
1554
by: Irfan Akram | last post by:
Hello People, I would appreciate your responses on this. I am writing an asp.net web-application involving C#. I am actually building a test hierarchy at the moment, which involves producing...
6
2741
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
8
2987
by: Jim in Arizona | last post by:
I've been using an example out of a book to be able to edit the rows in a database. I am getting the following error: ========================================================...
13
3960
by: royaltiger | last post by:
I am trying to copy the inventory database in Building Access Applications by John L Viescas but when i try to run the database i get an error in the orders form when i click on the allocate...
6
6518
by: blue875 | last post by:
A tale of two queries: One query looks like this, and runs fine. We'll call this the "Customer1 query": SELECT Customer1 overall.*, IIf(IsNull(.),0,1) AS IsField, IIf(IsNull(.),0,1) AS...
3
2084
by: visionstate | last post by:
Hi All, I have used the following article to help me build a query 'on the fly': http://www.fontstuff.com/access/acctut17.htm It's a very useful article and is exactly what I was looking for to...
1
2826
by: igor221189 | last post by:
Hello everyone. I have Access 2000 database which holds student records in the school.It stores subject grades for each student.In the 'Student Grade Form', I would like to search student surname...
0
7386
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,...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3236
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...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
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 ...
1
805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
459
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...

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.