943,752 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 1673
  • MS SQL RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 12th, 2008
0

sql - how to

Expand Post »
Hi,

I have two table like this:

Quote ...
quote_tb:

ID item vendor quote
11 a v1 100
11 a v2 200
11 b v1 400
11 b v2 300
11 c v1 555
11 c v2 777
22 a v1 122
22 a v2 222
22 b v1 322
22 b v2 422
22 c v1 555
22 c v2 777


vendor_tb

name phone# fax# address#
v1 1111 2222 3, x street
v2 1212 2323 4, m street
I am trying to write an sql that given me the following result:

Quote ...
lowestQ_tb

ID item vendor quote phone# fax#
11 a v1 100 1111 2222
11 b v2 300 1212 2323
11 c v1 555 1111 2222
i.e for each of the items a,b,c having the id(11) select the vendors that have the lease quote and then

get the phone and fax numbers of the vendors form the table vendor_tb.

To get the first part i.e. lowest quote for each item, this is the sql I use:

Quote ...
SELECT f.item, f.vendor, f.quote
FROM (SELECT item, min(quote) AS minprice FROM quote_tb WHERE ID='11' GROUP BY item)
AS x INNER JOIN quote_tb AS f ON f.item = x.item AND f.quote = x.minprice

This gives me the result:

ID item vendor quote

11 a v1 100
11 b v2 300
11 c v1 555
I don't know how to take it further form here. Please can you tell me how to get the above desired result i.e table lowestQ_tb. Is it possible to do such a thing?

Thanks.
Tara
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tara423 is offline Offline
6 posts
since Mar 2008
Mar 12th, 2008
0

Re: sql - how to

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.

MS SQL Syntax (Toggle Plain Text)
  1. SELECT
  2. q.ID,
  3. q.item,
  4. v.name,
  5. min(q.quote) minprice,
  6. v.phone#,
  7. v.fax#
  8. FROM
  9. quote_tb q JOIN vendor_tb v ON q.vendor = v.name
  10. GROUP BY
  11. q.ID,
  12. q.item,
  13. v.name,
  14. v.phone#,
  15. v.fax#

That gives ALL minimum quotes, if you want just a particular quote ID just add a where clause: where q.ID = 11
MS SQL Syntax (Toggle Plain Text)
  1. SELECT
  2. q.ID,
  3. q.item,
  4. v.name,
  5. min(q.quote) minprice,
  6. v.phone#,
  7. v.fax#
  8. FROM
  9. quote_tb q JOIN vendor_tb v ON q.vendor = v.name
  10. WHERE
  11. q.ID = 11
  12. GROUP BY
  13. q.ID,
  14. q.item,
  15. v.name,
  16. v.phone#,
  17. v.fax#
Last edited by hollystyles; Mar 12th, 2008 at 9:47 am.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Mar 12th, 2008
0

Re: sql - how to

Hmm re-reading your post that may not be quite what you want, you want lowest quote regardless of vendor I think.
MS SQL Syntax (Toggle Plain Text)
  1. SELECT
  2. q.[ID],
  3. q.item,
  4. v.[name],
  5. mq.minprice,
  6. v.phone#,
  7. v.fax#
  8. FROM
  9. quote_tb q
  10. JOIN
  11. (
  12. SELECT
  13. [ID],
  14. item,
  15. min(quote) minprice
  16. FROM
  17. quote_tb
  18. GROUP BY
  19. [ID], item
  20. ) mq ON q.[ID] = mq.[ID]
  21. AND q.item = mq.item
  22. AND q.quote = mq.minprice
  23. JOIN vendor_tb v ON q.vendor = v.[name]
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Mar 12th, 2008
0

Re: sql - how to

Thank you for the reply.

I tried the sql as you have given. It gives the phone and fax details of the vendors but it also lists down all the items. It is not selecting only those items that have the mim quote.

Kindly advice.

Thanks,
Tara
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tara423 is offline Offline
6 posts
since Mar 2008
Mar 12th, 2008
0

Re: sql - how to

OOPS. please ignore the above post. I did not see your 2nd post.

Thanks,
Tara
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tara423 is offline Offline
6 posts
since Mar 2008
Mar 12th, 2008
0

Re: sql - how to

Excellent. Please mark thread solved
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Mar 12th, 2008
0

Re: sql - how to

Mission unsuccessful!

For some reason, whatever way I try it doesn't seem to be working.

I am connecting to MS Access database through VC++. In whatever way I try to add an extra 'inner join' it gives me the error : "Syntax error(missing operator) in query expression". If I remove the extra 'inner join' its all fine.

Can't understand why. Is it something to do with the formatting?

Kindly advise.

Thanks,
Tara
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tara423 is offline Offline
6 posts
since Mar 2008
Mar 12th, 2008
0

Re: sql - how to

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.

MS SQL Syntax (Toggle Plain Text)
  1. SELECT quote_tb.ID, quote_tb.item, vendor_tb.name, mq.minprice, vendor_tb.[phone#], vendor_tb.[fax#]
  2. 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;
Last edited by hollystyles; Mar 12th, 2008 at 1:41 pm.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Mar 12th, 2008
0

Re: sql - how to

Oh my God! It worked! I can't tell you how very very happy I am.

Actually, I just assumed that that the sql MS Access uses is going to be same as MS sql

Thanks a billion for your patience and help.

Gosh! How do you make these sql statements? Its so confusing and complex to me

Once again, thank you for your help.

Tara.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tara423 is offline Offline
6 posts
since Mar 2008
Mar 13th, 2008
0

Re: sql - how to

Quote ...
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.

Quote ...
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.
Last edited by hollystyles; Mar 13th, 2008 at 5:56 am.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Credit Voucher System With Expiry Periods
Next Thread in MS SQL Forum Timeline: update table using function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC