Hello, I've searched for a couple of hours and am stuck. I have 3 tables, and need to count the unique values in one of them...
Table: SUPPLIER
SUPPLIER_ID |
SUPPLIER_NAME |
S-91 |
IBM |
S-92 |
Dell |
S-93 |
Lenovo |
S-94 |
Lenovo |
Table: POS
SUPPLIER_ID |
SITE |
PO_NUMBER |
ORDER_VALUE |
S-91 |
Location A |
451 |
$5 |
S-92 |
Location B |
452 |
$10 |
S-93 |
Location C |
453 |
$12 |
S-93 |
Location B |
454 |
$7 |
S-93 |
Location D |
455 |
$18 |
S-92 |
Location D |
456 |
$20 |
S-93 |
Location C |
457 |
$6 |
S-94 |
Location C |
458 |
$9 |
Table: SITE
SITE_DESCRIPTION |
COUNTRY |
Location A |
USA |
Location B |
Switzerland |
Location C |
Switzerland |
Location D |
Brazil |
I want to be able to use the SUPPLIER as a criteria, and count the number of unique Buying Locations being used for each country. For example, if I was looking at Lenovo, I'd want to see that Switerland is buying Lenovo products from 2 sites (Locations B and C) and Brazil is buying from 1 site (Location D):
Switzerland - 2
Brazil - 1
I've gotten it to work using a nested SELECT statement, but it's counting all of the times the supplier name is listed in the PURCHASE_ORDERS table (it's giving me 4 for Switzerland, not 2.) Here's what I'm using:
Code:
SELECT DISTINCT SUPPLIER.SUPPLIER_NAME, SITE.COUNTRY, Count(*) AS Number_of_Sites
FROM (POS LEFT JOIN (SITE LEFT JOIN (SELECT DISTINCT SITE_DESCRIPTION FROM SITE)
AS T ON SITE.SITE_DESCRIPTION = T.SITE_DESCRIPTION)
ON POS.SITE_ID = SITE.SITE_ID)
RIGHT JOIN SUPPLIER ON POS.SUPPLIER_ID = SUPPLIER.SUPPLIER_ID
GROUP BY SUPPLIER.SUPPLIER_NAME, SITE.COUNTRY
HAVING (((SUPPLIER.SUPPLIER_NAME)="Lenovo"));
Any help would be greatly appreciated!