I have two tables with customer order information: Orders and LineItem. I also have a table of employees who help place the orders.
I am trying to create a view that shows the Orders.OrderID and Orders.CreateDate of the FIRST order placed for each Orders.EmpID (so, the first time an employee processed an order, and what the Order ID was).
Here's what I have so far:
SELECT Orders.EmpID, COUNT(distinct Orders.OrderID) AS Orders, MIN(LineItem.CreateDate) AS FirstOrderDate
FROM Orders, LineItem
WHERE LineItem.OrderID = Orders.OrderID
and LineItem.ItemStatus = 'shipped'
GROUP BY Orders.EmpID
This gives me how many Orders each employee has processed, and the date of their first order. But how do I then get the ID of the order with that CreateDate? Because of shipping delays and cancellations, the lowest OrderID isn't always the first Order processed (and so using MIN(Orders.OrderID) wouldn't necessarily match the correct OrderID with the MIN(LineItem.CreateDate).
I could build this into a table using a temp table easily enough, but the goal is to be able to use this information in a weekly Crystal Report. If I can get the information in a view, we won't have to constantly update the table, not to mention the massive amount of Order data I'd rather not store again.
I am trying to create a view that shows the Orders.OrderID and Orders.CreateDate of the FIRST order placed for each Orders.EmpID (so, the first time an employee processed an order, and what the Order ID was).
Here's what I have so far:
SELECT Orders.EmpID, COUNT(distinct Orders.OrderID) AS Orders, MIN(LineItem.CreateDate) AS FirstOrderDate
FROM Orders, LineItem
WHERE LineItem.OrderID = Orders.OrderID
and LineItem.ItemStatus = 'shipped'
GROUP BY Orders.EmpID
This gives me how many Orders each employee has processed, and the date of their first order. But how do I then get the ID of the order with that CreateDate? Because of shipping delays and cancellations, the lowest OrderID isn't always the first Order processed (and so using MIN(Orders.OrderID) wouldn't necessarily match the correct OrderID with the MIN(LineItem.CreateDate).
I could build this into a table using a temp table easily enough, but the goal is to be able to use this information in a weekly Crystal Report. If I can get the information in a view, we won't have to constantly update the table, not to mention the massive amount of Order data I'd rather not store again.