Home > other >  I am trying to find the sql query to get the red boxed items from the diagram. What is the query to
I am trying to find the sql query to get the red boxed items from the diagram. What is the query to

Time:03-22

enter image description here

I have been trying to the sql query for the columns in the red box, any assistance would be helpful.

I tried this query:

SELECT

Employees.LastName,

Employees.FirstName,

Region.RegionDescription

FROM

    (
    
        (
    
            (Employees 
    
                LEFT JOIN
    
                EmployeeTerritories 
    
                    ON Employees.EmployeeID = EmployeeTerritories.EmployeeID) 
    
        LEFT JOIN
    
        Territories 
    
            ON EmployeeTerritories.EmployeeID = Territories.TerritoryID) 
    
    LEFT JOIN
    
      Region 
    
        ON Territories.RegionID = Region.RegionID
    
    ); 

CodePudding user response:

Your join with Territories was on EmployeeID (EmployeeTerritories.EmployeeID = Territories.TerritoryID), but should be on EmployeeTerritories.TerritoryID = Territories.TerritoryID. And removed all the brackets, makes it easier to read. Does this work for you?

SELECT
  Employees.LastName,
  Employees.FirstName,
  Region.RegionDescription
FROM Employees 
LEFT JOIN EmployeeTerritories 
ON Employees.EmployeeID = EmployeeTerritories.EmployeeID
LEFT JOIN Territories 
ON EmployeeTerritories.TerritoryID = Territories.TerritoryID
LEFT JOIN Region 
ON Territories.RegionID = Region.RegionID

CodePudding user response:

please check second join

ON EmployeeTerritories.EmployeeID = Territories.TerritoryID) should be ON EmployeeTerritories.TerritoryID = Territories.TerritoryID)

here is changed SQL

   SELECT
    Employees.LastName,    
    Employees.FirstName,    
    Region.RegionDescription    
    FROM    
        (
          (        
                (Employees         
                    LEFT JOIN
                    EmployeeTerritories         
                        ON Employees.EmployeeID = EmployeeTerritories.EmployeeID)         
            LEFT JOIN        
            Territories         
                ON EmployeeTerritories.TerritoryID = Territories.TerritoryID)         
        LEFT JOIN        
          Region         
            ON Territories.RegionID = Region.RegionID        
        ); 
  • Related