Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible Circular Reference in View

Status
Not open for further replies.

Zarobi

Technical User
Feb 19, 2004
13
US
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.
 
Untested but this should work, let me know

Code:
SELECT o.EmpID, l.Orders, l.FirstOrderDate
FROM (
SELECT OrderID, MIN(CreateDate) AS FirstOrderDate,COUNT(distinct OrderID) AS Orders
FROM  LineItem
WHERE ItemStatus = 'shipped'
GROUP BY OrderID) l join Orders o on l.OrderID = o.OrderID

Denis The SQL Menace
SQL blog:
Personal Blog:
 


try this:

Code:
select t0.EmpID, t0.orders, t0.FirstOrderDate, t1.OrderID as firstProcessedOrder 
( 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 ) t0 inner join 
( SELECT Orders.EmpID, 
         Orders.OrderID, 
         LineItem.CreateDate AS FirstOrderDate
FROM Orders, LineItem
WHERE LineItem.OrderID = Orders.OrderID
and LineItem.ItemStatus = 'shipped' ) t1
on t0.EmpID = t1.EmpID and t0.FirstOrderDate = t1.FirstOrderDate
 
It never crossed my mind to next tables in a view (obviously). Both solutions were helpful, and the view is working. Thank you very much!
 

Zarobi , just curious, do you mean above 2 queries return the same results?
 
No, just that I used ideas from both queries for my solution. My original post was a simplified version. The end result ended up using a left join, but was very similar to the second suggestion. Once I realized I could nest table queries in a view, I had everything I needed.

Again, thanks so much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top