473,322 Members | 1,562 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,322 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 5720
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.