Article ID: 114592
Article Last Modified on 1/8/2003
Examine all possible pairs of rows in Employees -- one from the first copy and one from the second copy. Then retrieve the names of both the employee and his or her manager if and only if the value in the Reports To field in one copy matches that of the Employee ID field in the second copy.
SELECT DISTINCTROW Employees_1.[Employee ID], Employees_1.[First Name], Employees_1.[Last Name], Employees_2.[First Name] AS [Manager FirstName], Employees_2.[Last Name] AS [Manager LastName] FROM Employees AS Employees_1, Employees AS Employees_2, Employees_1 INNER JOIN Employees_2 ON Employees_1.[Reports To] = Employees_2.[Employee ID];Or you could have simply aliased only the second or duplicate copy of the Employees table:
SELECT DISTINCTROW Employees.[Employee ID], Employees.[First Name], Employees.[Last Name], Employees_Dup.[First Name] AS [Manager FirstName], Employees_Dup.[Last Name] AS [Manager LastName] FROM Employees, Employees AS Employees_Dup, Employees INNER JOIN Employees_Dup ON Employees.[Reports To] = Employees_Dup.[Employee ID];Also, you don't really need to alias the columns or fields returned from the second copy of the table to disambiguate those fields from those in the first copy, because the use of the As <aliasname> on the table name does that for you. However, to make the results more meaningful, it is helpful to alias the field or column names as well.
SELECT DISTINCTROW Employees.[Employee ID], Employees.[First Name], Employees.[Last Name], Employees_Dup.[First Name], Employees_Dup.[Last Name] FROM Employees, Employees AS Employees_Dup, Employees INNER JOIN Employees_Dup ON Employees.[Reports To] = Employees_Dup.[Employee ID];
Sub Command1_Click ()
Dim db As database
Dim ds As dynaset
Dim sql As String
sql = sql & "SELECT DISTINCTROW Employees_1.[Employee ID],"
sql = sql & "Employees_1.[First Name], Employees_1.[Last Name],"
sql = sql & "Employees_2.[First Name] AS [Manager FirstName],"
sql = sql & "Employees_2.[Last Name] AS [Manager LastName]"
sql = sql & "FROM Employees AS Employees_1,Employees AS Employees_2,"
sql = sql & "Employees_1 INNER JOIN Employees_2 ON "
sql = sql & "Employees_1.[Reports To] = Employees_2.[Employee ID]"
Set db = OpenDatabase("c:\access\nwind.mdb")
Set ds = db.CreateDynaset(sql)
Do Until ds.EOF
' Enter the following three lines of code as one, single line:
Print "Employee "; ds![Employee ID], ds![First Name],
ds![Last Name], "Managed by "; ds![Manager FirstName],
ds![Manager LastName]
ds.MoveNext
Loop
ds.Close
db.Close
End Sub
Additional query words: 3.00
Keywords: KB114592