Write a query to display the manager name, department name, department phone number,…
- User
Write a query to display the manager name, department name, department phone number, employee name, customer name, invoice date, and invoice total for the department manager of the employee who made a sale to a customer whose last name is Hagan on May 18, 2015 (Figure P7.64). I don't have manager table get data from lg table
- Assistant
Since you don't have a manager table, I will assume that the manager information is also stored in the same table as the employee. Based on this assumption, here's a query that will display the required information:
sqlSELECT E1.employee_name AS manager_name, E1.department_name, E1.department_phone_number, E2.employee_name, C.customer_name, I.invoice_date, I.invoice_total FROM employees E1 JOIN employees E2 ON E1.employee_id = E2.manager_id JOIN customers C ON E2.employee_id = C.employee_id JOIN invoices I ON C.customer_id = I.customer_id WHERE C.customer_name LIKE '%Hagan%' -- Assuming 'Hagan' is the last name AND I.invoice_date = '2015-05-18'Note: In this query, I have used the assumption that the employee table has a column called "manager_id" that identifies the manager for each employee. Adjust the column names in the query based on your actual table schema.
- User
Select CONCAT(m.Emp_Fname, ' ', m.Emp_Lname) As ManagerName, d.Dept_Name, d.Dept_Phone, CONCAT(e.Emp_Fname, ' ', e.Emp_Lname) As EmployeeName, CONCAT(c.Cust_Fname, ' ', c.Cust_Lname) As CustomerName, i.Inv_Date, i.Inv_Total From LGDEPARTMENT d, LGEMPLOYEE m, LGEMPLOYEE e, LGCUSTOMER c, LGINVOICE i Where d.Emp_Num = m.Emp_Num And e.Emp_Num = i.Employee_ID And d.Dept_Num = e.Dept_Num And i.Cust_Code = c.Cust_Code And c.Cust_Lname = 'hagan' and i.inv_date = '2013-05-18' get results by using tsql
- Assistant
To convert the given query into T-SQL syntax, you can make the following adjustments:
sqlSELECT CONCAT(m.Emp_Fname, ' ', m.Emp_Lname) AS ManagerName, d.Dept_Name, d.Dept_Phone, CONCAT(e.Emp_Fname, ' ', e.Emp_Lname) AS EmployeeName, CONCAT(c.Cust_Fname, ' ', c.Cust_Lname) AS CustomerName, i.Inv_Date, i.Inv_Total FROM LGDEPARTMENT d JOIN LGEMPLOYEE m ON d.Emp_Num = m.Emp_Num JOIN LGEMPLOYEE e ON d.Dept_Num = e.Dept_Num JOIN LGCUSTOMER c ON i.Cust_Code = c.Cust_Code JOIN LGINVOICE i ON e.Emp_Num = i.Employee_ID WHERE c.Cust_Lname = 'Hagan' AND i.Inv_Date = '2013-05-18'Please note that this query assumes that you have the required tables (LGDEPARTMENT, LGEMPLOYEE, LGCUSTOMER, LGINVOICE) with the specified column names. Adjust the column and table names according to your actual schema.
- User
The multi-part identifier "i.Cust_Code" could not be bound.
- Assistant
Apologies for the oversight. It seems that the column
Cust_Codeis missing from the join condition in the query. Here's the corrected query:sqlSELECT CONCAT(m.Emp_Fname, ' ', m.Emp_Lname) AS ManagerName, d.Dept_Name, d.Dept_Phone, CONCAT(e.Emp_Fname, ' ', e.Emp_Lname) AS EmployeeName, CONCAT(c.Cust_Fname, ' ', c.Cust_Lname) AS CustomerName, i.Inv_Date, i.Inv_Total FROM LGDEPARTMENT d JOIN LGEMPLOYEE m ON d.Emp_Num = m.Emp_Num JOIN LGEMPLOYEE e ON d.Dept_Num = e.Dept_Num JOIN LGINVOICE i ON e.Emp_Num = i.Employee_ID JOIN LGCUSTOMER c ON i.Cust_ID = c.Cust_ID -- Adjust the join condition based on your actual column names WHERE c.Cust_Lname = 'Hagan' AND i.Inv_Date = '2013-05-18'Make sure to modify the join condition
ON i.Cust_ID = c.Cust_IDto match your actual column names in theLGINVOICEandLGCUSTOMERtables.