Arg81
Well-known member
Hi,
I'm trying to write an SQL query that lists all Customers that haven't had any work set to them.
I've got the basics, but there's a "middleman" table of Customer Sites.
This is a basic DB layout:
Customer (can have many) CustomerSites
Development
Samples
A customer site is set to the Dev or Samples table.
Here is what I have
So it looks at all the rows in the Devs, and all the rows in the Samples, and lists all Customer sites that aren't listed in them.
It then lists the Customer Name for these Customer Sites.
HOWEVER (this is where I've come undone so to say)
A customer can have many sites. If for instance the customer has 4 sites, and only 1 of them hasn't been used, I DONT want the Customer Name to show.
The query above will list the Customer Name even if other sites have been used.
I'm trying to write an SQL query that lists all Customers that haven't had any work set to them.
I've got the basics, but there's a "middleman" table of Customer Sites.
This is a basic DB layout:
Customer (can have many) CustomerSites
Development
Samples
A customer site is set to the Dev or Samples table.
Here is what I have
VB.NET:
SELECT C.CustomerID, C.CustomerName
FROM Customer AS C
LEFT OUTER JOIN CustomerSite AS CS ON C.CustomerID = CS.CustomerID
LEFT OUTER JOIN Sample AS SR ON CS.CustomerSiteID = SR.CustomerSiteID
LEFT OUTER JOIN Development AS D ON CS.CustomerSiteID = D.CustomerSiteID
WHERE (D.DevID IS NULL) AND (SR.SamID IS NULL)
ORDER BY C.CustomerName
So it looks at all the rows in the Devs, and all the rows in the Samples, and lists all Customer sites that aren't listed in them.
It then lists the Customer Name for these Customer Sites.
HOWEVER (this is where I've come undone so to say)
A customer can have many sites. If for instance the customer has 4 sites, and only 1 of them hasn't been used, I DONT want the Customer Name to show.
The query above will list the Customer Name even if other sites have been used.