I have a database with information about customers, sites, and servers. Here are the tables involved:
CMCustomer: CustomerID and customer name
CMSite: SiteID and Site name
CMSiteServer: SiteServerID, SiteID, ServerName
CMCustomerSiteServer: CustomerID and SiteServerID
For a particular site, this retrieves a list of customers and Customer IDs:
SELECT c.CustomerName, c.CustomerID
FROM (CMSite s INNER JOIN CMSiteCustomer sc ON s.SiteID = sc.SiteID) INNER JOIN CMCustomer c ON sc.CustomerID = c.CustomerID
WHERE s.SiteID = 13 AND sc.ModifiedBy >= 0
ORDER BY c.CustomerName
Each site has multiple servers (a server can be in only one site). In CMCustomerSiteServer, there may or may not be a row for any particular customer/Siteserver, and that's causing the problem. This query returns the customers and server names where there is a row, but I want to return the customer names and IDs even if there is no matching row in CMCustomerSiteServer.
select c.Customername, c.CustomerID, ss.Name from cmcustomersiteserver cs (nolock)
inner join CMSiteServer ss ON cs.SiteServerID = ss.SiteServerID
inner join CMCustomer c ON c.CustomerID = cs.CustomerID
where ss.SiteID = 11
order by c.Customername
The left outer joins I've tried return no rows in cases where there is no CMCustomerSiteServer.
So basically, I want Customer name and ID to return rows whether or not there's a match in CMCustomerSiteServer.
Thanks.