472,796 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Combine these two Queries?

Is there any way to combine these two queries into one? I have tables
Item_Master & Kit_Master which are the source tables. Query 2 is using
both Item_Master (table) & qryKit1 (query) & Kit_Master (table) as its
record source because the Kit_Master table has 2 fields in it that get
joined back to the same field in Item_Master.

Item_Master key field is SKU
Kit_Master key fields are KitNo & ComponentNo

Item_Master - SKU contains a record for each Kit_Master - KitNo and
also for each Kit_Master - ComponentNo

qryKit1:
-----------
SELECT Item_Master.SKU, Item_Master.OnHand
FROM Item_Master

qryKit2:
-----------
SELECT Kit_Master.KitNo, Min(Item_Master.OnHand/[Qty]) AS Availability
FROM (Kit_Master INNER JOIN Item_Master ON Kit_Master.ComponentNo =
Item_Master.SKU) INNER JOIN qryKit1 ON Kit_Master.KitNo = qryKit1.SKU
GROUP BY Kit_Master.KitNo;

I can use two queries if I need to, I was just wondering if it could be
done. Thanks.

Aug 3 '06 #1
4 5679
I'm sure it is possible to combine your queries, but in order to make
the results come out as you desire you should post 3 samples of your
data -

1st - a few rows from the primary table

2nd - a few of the corresponding rows from the 2nd table

3rd - the rows from the desired resultset

So run your queries from your source tables and then show a few rows
from the resulting query. Then it will be easier to modify your queries
into 1 query.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Aug 3 '06 #2
Thanks for the response, here's the info you requested.

Item_Master (qryKit1 is the same)
---------------------------------------
SKU OnHand
55-1513-01 6600
69-0044-01 0
71-0831-01 51
84-0072-01 0
84-0113-01 59
501-00007-02 70
502-00008-01 438
55-0722-01 0
55-0840-01 65
55-0841-01 150
55-0941-01 224
55-1254-02 231
55-1255-01 199
55-1256-01 0
55-1257-02 80
55-1258-01 70
55-1259-01 32
55-1469-02 164
55-1513-01A 3550
55-1513-01B 452
55-1513-01C 2200
T NUTS 0
Kit_Master
---------------------------------------------------------------------
KitNo ComponentNo Qty
55-1513-01 55-1513-01A 1
55-1513-01 55-1513-01B 1
55-1513-01 55-1513-01C 1
69-0044-01 502-00008-01 1
69-0044-01 55-1254-02 1
69-0044-01 55-1256-01 1
69-0044-01 55-1258-01 1
69-0044-01 55-1259-01 1
71-0831-01 501-00007-02 1
71-0831-01 502-00001 1
71-0831-01 55-1469-02 1
84-0072-01 55-0722-01 1
84-0072-01 55-0840-01 2
84-0072-01 55-0841-01 4
84-0072-01 55-0941-01 1
84-0072-01 55-1254-01 1
84-0072-01 55-1255-01 1
84-0072-01 55-1256-01 1
84-0072-01 55-1257-02 1
84-0072-01 55-1258-01 1
84-0072-01 55-1259-01 1
84-0072-01 T NUTS 4
84-0113-01 55-0584-01 1
84-0113-01 55-1154-01 1
84-0113-01 55-1155-01 1
qryKit1:
---------------------------------------
SKU OnHand
55-1513-01 6600
69-0044-01 0
71-0831-01 51
84-0072-01 0
84-0113-01 59
qryKit2: (result I'm looking for - Availabilty)
---------------------------------------
KitNo Availability
55-1513-01 452
69-0044-01 0
71-0831-01 70
84-0072-01 0
84-0113-01 0
Rich P wrote:
I'm sure it is possible to combine your queries, but in order to make
the results come out as you desire you should post 3 samples of your
data -

1st - a few rows from the primary table

2nd - a few of the corresponding rows from the 2nd table

3rd - the rows from the desired resultset

So run your queries from your source tables and then show a few rows
from the resulting query. Then it will be easier to modify your queries
into 1 query.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Aug 3 '06 #3
Thanks. I was able to reproduce your queries with your data (thanks for
providing the original sql code, also) and I was able to get the same
query results as you using your query sql. Then I was able to modify
your query as follows.

This query should do the trick for you. Just paste the sql into a query
window.

SELECT t1.KitNo, Min(t2.OnHand/[Qty]) AS Availability
FROM (Kit_Master t1 INNER JOIN Item_Master t2 ON t1.ComponentNo =
t2.SKU) INNER JOIN (SELECT Item_Master.SKU, Item_Master.OnHand
FROM Item_Master) t3 ON t1.KitNo = t3.SKU
GROUP BY t1.KitNo;

BTW, if someone ever asks for data, it is always best to provide it in
DDL format

--------------------------------------
Create Table Item_Master(SKU Text(50), OnHand Integer)
Insert Into Item_Master
Select '55-1513-01', 6600 Union
Select '69-0044-01, 0 Union
...
----------------------------------------

I had to parse out all of your data by hand. The above code would be
run in VBA using DoCmd.RunSql. But hey, just glad to help.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Aug 3 '06 #4
Thanks, that's exactly what I was looking for!! And now that I see how
it's done, I'll be able to use it in future queries. Thanks again for
taking the time to help.

Rich P wrote:
Thanks. I was able to reproduce your queries with your data (thanks for
providing the original sql code, also) and I was able to get the same
query results as you using your query sql. Then I was able to modify
your query as follows.

This query should do the trick for you. Just paste the sql into a query
window.

SELECT t1.KitNo, Min(t2.OnHand/[Qty]) AS Availability
FROM (Kit_Master t1 INNER JOIN Item_Master t2 ON t1.ComponentNo =
t2.SKU) INNER JOIN (SELECT Item_Master.SKU, Item_Master.OnHand
FROM Item_Master) t3 ON t1.KitNo = t3.SKU
GROUP BY t1.KitNo;

BTW, if someone ever asks for data, it is always best to provide it in
DDL format

--------------------------------------
Create Table Item_Master(SKU Text(50), OnHand Integer)
Insert Into Item_Master
Select '55-1513-01', 6600 Union
Select '69-0044-01, 0 Union
..
----------------------------------------

I had to parse out all of your data by hand. The above code would be
run in VBA using DoCmd.RunSql. But hey, just glad to help.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Aug 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Lutka | last post by:
I've been racking my brains all day over this. And I'm not the best at SQL either. I need a query that will produce the following results:...
5
by: Jamie Pittman via AccessMonster.com | last post by:
I have two tables with 5000 entries on them. One is based for regular time with several variables example (employee name, date,time in and out, code, customer, building) I have another table that...
1
by: Will | last post by:
Hi all I have a table, where there's AutoID, Name of Person (looked up from tblPerson), Name of Dispute (looked up from tblDispute), Date, Details (Memo). What I have is another table that need...
4
by: PlayHard | last post by:
I want my output in two columns like this: C_INCIDENT_TYPE \ TOTAL COUNT How do I combine my two queries to get result. First Query SELECT C_INCIDENT_TYPE, COUNT (*)
46
by: dude88888 | last post by:
Hello I have one query where i get the total number of units for an entire fund say fund 89 has 1000 total units Then i need to find out how much of the total fund each user has say user...
4
by: | last post by:
I have query1 where I set a date range. Query2 is a crosstab query and is based on query1. I don't want the queries to be saved in the mdb but I do want them to be run in VBA using a querystring. If...
1
by: csolomon | last post by:
Hello: I am using two queries to get one result set. The issue is, I return no data when I combine them into one query. I have listed both queries, as well as the 3rd query that shows them...
2
by: bips2005 | last post by:
i have got two queries, $sql1 = "select cust_id from customer where comp_name = '$compname'"; $result = $DB->query($sql1); $result->fetchInto($row); $sql = "select expirydate from...
13
by: MNNovice | last post by:
I have three tables, tblAP, tblPayroll and tblAllocation. Each records three separate expenses. All three are related by FK ECHOID, each has common fields such as AccountNo, FundNo, GrantNo,...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.