Firstly just join the quote_tb to the vendor_tb. That gives the vendor details for ALL quotes.
Then use GROUP BY (to merge up all the repeating colums (ID, item, name, phone#, fax#) and the aggragate function MIN on the quote column to reduce the result set to just the lowest (MIN) quote amounts.
select
q.ID,
q.item,
v.name,
min(q.quote) minprice,
v.phone#,
v.fax#
from
quote_tb q join vendor_tb v on q.vendor = v.name
group by
q.ID,
q.item,
v.name,
v.phone#,
v.fax#
That gives ALL minimum quotes, if you want just a particular quote ID just add a where clause: where q.ID = 11
select
q.ID,
q.item,
v.name,
min(q.quote) minprice,
v.phone#,
v.fax#
from
quote_tb q join vendor_tb v on q.vendor = v.name
where
q.ID = 11
group by
q.ID,
q.item,
v.name,
v.phone#,
v.fax#
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
Hmm re-reading your post that may not be quite what you want, you want lowest quote regardless of vendor I think.
select
q.[ID],
q.item,
v.[name],
mq.minprice,
v.phone#,
v.fax#
from
quote_tb q
join
(
select
[ID],
item,
min(quote) minprice
from
quote_tb
group by
[ID], item
) mq on q.[ID] = mq.[ID]
and q.item = mq.item
and q.quote = mq.minprice
join vendor_tb v on q.vendor = v.[name]
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
Excellent. Please mark thread solved :)
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
MS Access !! yuck !! You didn't say you were using Access, and as this is a MS SQL Forum I used TSQL.
Ok just for you I have recreated everything in Access and tweaked the SQL. Basically removed all formating, changed joins to INNER JOIN, added the AS keword where aliases were being used , bracketed the multiple column joins between quote_tb and the sub-query table mq and added the trailing semi-colon.
SELECT quote_tb.ID, quote_tb.item, vendor_tb.name, mq.minprice, vendor_tb.[phone#], vendor_tb.[fax#]
FROM (quote_tb INNER JOIN (select [ID], item, min(quote) as minprice from quote_tb group by [ID], item) mq ON (quote_tb.quote = mq.minprice) AND (quote_tb.item = mq.item) AND (quote_tb.ID = mq.ID)) INNER JOIN vendor_tb ON quote_tb.vendor = vendor_tb.name;
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
Actually, I just assumed that that the sql MS Access uses is going to be same as MS sql
There is a SQL standard but each implementatin has it's own quirks on top of that. The only cure for this is experience. INNER JOIN is the assumed default in TSQL so you can be lazy and just type JOIN, Access is less forgiving I guess but essentially they are the same.Gosh! How do you make these sql statements? Its so confusing and complex to me
He he, yes it's like that for all of us, just got to keep reading and learning and using. Again experience will eventually win out.
I got to the final solution not because I know Access's SQL quirks of by heart. I created the subquery first in design view in Access (the group by on quote_db) and saved it. Then created a new one in design view, added the quote_tb and vendor-tb tables and then the subquery I had just saved previously, dragged the columns between them with the mouse to create the joins, then from the view menu chose SQL View. That reminded me how much Access likes brackets! then I just merged the SQL View of the seperate subquery into the main query using cut and paste to make it all one query statement.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68