dianemarie
Instructor
Hello. I’m trying to retrieve Last Broker for a particular order. I have an order history table (OH) with the usual info but for purposes here the three relevant fields are: Order#, Customer, Broker. A customer has many orders, and a customer also can use different brokers for each order. I am summarizing by customer (quantity) but I want to pull only the most recent broker that the customer used. This can always be accomplished by looking at max(order#) or max(OrdDate) – same diff. Right now, I have a temp table @T that pulls the most recent order for each customer, returning order#, broker# and customer# for the most recent order.
select max(OrdNum) as OrdNum,BrokerNum,CustNum
from nm_OrdHistAll OH
group by CustNum,BrokerNum
I thought I could link @T to OH this way as shown below, and then pull the field (LastBroker) for each order from the t.BrokerNum.
left outer join @T t on t.BrokerNum = oh.BrokerNum
What I expected to see in my result set is shown in the example below. But the LastBroker is not working (I’m still seeing the broker on the order) and also I’m getting duplicate records. Obviously I’m going about this wrong. Any help would be greatly appreciated.
OrdDate/oh.OrdNum/ oh.CustNum/oh.BrokerOnOrder /t.LastBroker
01/01/09 123 ABC Co. BrokerA BrokerC
01/05/09 128 ABC Co. BrokerB BrokerC
01/10/09 132 ABC Co. BrokerA BrokerC
02/06/09 155 ABC Co. BrokerC BrokerC
select max(OrdNum) as OrdNum,BrokerNum,CustNum
from nm_OrdHistAll OH
group by CustNum,BrokerNum
I thought I could link @T to OH this way as shown below, and then pull the field (LastBroker) for each order from the t.BrokerNum.
left outer join @T t on t.BrokerNum = oh.BrokerNum
What I expected to see in my result set is shown in the example below. But the LastBroker is not working (I’m still seeing the broker on the order) and also I’m getting duplicate records. Obviously I’m going about this wrong. Any help would be greatly appreciated.
OrdDate/oh.OrdNum/ oh.CustNum/oh.BrokerOnOrder /t.LastBroker
01/01/09 123 ABC Co. BrokerA BrokerC
01/05/09 128 ABC Co. BrokerB BrokerC
01/10/09 132 ABC Co. BrokerA BrokerC
02/06/09 155 ABC Co. BrokerC BrokerC