Trouble writing a SELECT query for the following table

Go To StackoverFlow.com

0

Possible Duplicate:
Writing an SQL query to SELECT item from the following table

I am trying to list the names of the suppliers (sname) and part number (pnum) of suppliers who had a shipment of at least two different parts.

Here are the two tables I need to work with...

Shipments Table: http://i44.tinypic.com/1zdcc9j.jpg

Suppliers Table: http://i39.tinypic.com/o6w414.png

What I tried:

This is incorrect, I tried something along these lines...

SELECT snum 
FROM (SELECT snum, count(snum) AS nbr FROM Shipments ) 
WHERE nbr > 1;

Thank you

2012-04-05 02:23
by CSoverIT
Why is this question tagged as mysql and access - Mosty Mostacho 2012-04-05 02:29
b/c I am using access to implement this sql query - CSoverIT 2012-04-05 02:34
First, please don't put tag info in the subject line; that's what tags are designed to do. (The fact you used SELECT and tagged MySQL and SQL makes SQL redundant in the subject, and the extra phrasing obscures the question.) Second, you need to edit to provide the table info here; off-site links for essential information means that your question becomes meaningless if those external sites are off-line or disappear. It also makes your question non-searchable for future users. Thanks. : - Ken White 2012-04-05 02:35


0

SELECT suppliers.sname, shipments.pnum
FROM suppliers, shipments
WHERE shipments.snum = suppliers.snum
GROUP BY suppliers.sname
HAVING count(shipments.pnum) >= 2
2012-04-05 02:30
by Andreas Wong
thank you! I appreciate the reply, this does not work for some reason :/ it makes complete sense to me though. I am getting an error in access when I try to run this query saying that 'sname' isn't included as part of the aggregate function - CSoverIT 2012-04-05 02:39
@CSoverIT Hmm are you getting syntax error - Andreas Wong 2012-04-05 02:40
@CSoverIT I used >= instead of >, maybe that's the cause, please check : - Andreas Wong 2012-04-05 02:41
Nope, I tried that fix, not working : - CSoverIT 2012-04-05 02:45
@CSoverIT care to elaborate what you mean by not working? Returning wrong results, or - Andreas Wong 2012-04-05 02:46
no I am getting an error, it won't return any results at all, the error says that attributes used such as 'sname' and 'pnum' aren't part of the aggregate function - CSoverIT 2012-04-05 02:47
are you running this query on access? I tried on mysql and it doesn't complain about that = - Andreas Wong 2012-04-05 02:53
Yes on Access. - CSoverIT 2012-04-05 02:58
:S... Does it work if you remove the HAVING clause altogether (albeit I know wrong results, but try first - Andreas Wong 2012-04-05 03:14


0

You need to be using a GROUP BY clause when using an aggregate function such as COUNT. When using Aggregates and GROUP BY the HAVING CLAUSE acts as your where clause.

This will get you all snums that have a count > 1

SELECT snum
FROM
(
    SELECT snum, count(snum) AS nbr
    FROM Shipiments
    GROUP BY snum
    HAVING COUNT(snum) > 1
) AS T

This will join the table back to the suppliers table to get the supplier name

SELECT DISTINCT Suppliers.sname
FROM
(
    SELECT snum, count(snum) AS nbr
    FROM Shipiments
    GROUP BY snum
    HAVING COUNT(snum) > 1
) AS T INNER JOIN Suppliers ON Suppliers.snum = T.snum
2012-04-05 02:31
by Dan P
That does not list the name, only the number... I am trying to figure out how to link the name of the supplier to the item listed, thank you though - CSoverIT 2012-04-05 02:43
Try the bottom query that was added. Assuming the join is correct it will give you all supplier names that have 2 or more shipments - Dan P 2012-04-05 02:49


0

I don't know about Access (as this question is tagged as such), but this should work in MySQL:

select su.sname from suppliers su
join shipments sh on su.snum = sh.snum
group by su.sname
having count(distinct su.pnum) >= 2
2012-04-05 02:34
by Mosty Mostacho
Ads