| Author | Topic | 
                            
                                    | SmokeyStarting Member
 
 
                                        3 Posts | 
                                            
                                            |  Posted - 2013-08-27 : 20:56:43 
 |  
                                            | Hi AllI have 2 tables in SQL Server 2005 with a one to many relationship and I need to pull the latest of several records of the desired status from the second table. Basically the main table is the order header and the second table has a record for each status change of which there can be more than one of each eg an order can be modified hence has a second confirmation.The tables are linked by OrderNum and I need to pull the latest entry with a status of Confirm. Initial selection is based on OrderTable.OrderDate plus some other criteria not really relevant to this question.Here is some pseudo input & output data which I think will explain what I'm trying to do. OrderHeaderTable HOrderNum  OrderDate  Customer123       10/5/2013  Cust1456       10/5/2013  Cust2789       10/5/2013  Cust3 OrderStatusTable SOrderNum  Modified          ModifiedBy  Status123       15/5/2013  10:55  Fred        Open123       15/5/2013  11:00  Fred        Confirm123       15/5/2013  11:05  Fred        Compl456       15/5/2013  15:00  Tom         Confirm456       15/5/2013  15:00  Tom         Confirm456       16/5/2013  09:00  Dick        Confirm789       15/5/2013  10:00  Harry       ConfirmAs I'm only looking at the latest Confirm status, the result I'm after is : OrderTable  OrderTable   OrderTable  StatusTable      StatusTable   StatusTableH.OrderNum  H.OrderDate  H.Customer  S.Modified       S.ModifiedBy  S.Status123         10/5/2013    Cust1       15/5/2013 11:00  Fred          Confirm456         10/5/2013    Cust2       16/5/2013 09:00  Dick          Confirm789         10/5/2013    Cust3       15/5/2013 10:00  Harry         Confirm/* 123 the only Confirm record for this order number, ignores Open & Compl status *//* 456 the latest Confirm record for this order with the associated ModifiedBy name *//* 789 the only Confirm record for this order number */I've found quite a few solutions online that look close but I don't seem to be able to translate those into what I actually need.I've been trying to resolve this for a couple of months now so any help would be greatly appreciated.Mark |  | 
       
                            
                       
                          
                            
                                    | stepsonAged Yak Warrior
 
 
                                    545 Posts | 
                                        
                                          |  Posted - 2013-08-28 : 00:17:45 
 |  
                                          | [code]select 	H.OrderNum	,H.orderDate	,H.Customer	,S.Modified	,S.ModifiedBy	,S.Statusfrom OrderHeaderTable Hcross apply	(select top 1		S.Modified,		S.ModifiedBy,		S.Status	 from OrderStatusTable S	 where H.OrderNum=S.OrderNum		and S.Status='Confirm'	 order by Modified desc) S[/code]SCe-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mutsabinWeb |  
                                          |  |  | 
                            
                       
                          
                            
                                    | SmokeyStarting Member
 
 
                                    3 Posts | 
                                        
                                          |  Posted - 2013-08-28 : 01:19:19 
 |  
                                          | Hey Stepson, that nailed it, thanks heaps.I knew Top would come into it but I don't think I've seen Cross Apply before, I've got some study to do.Thanks again.Mark |  
                                          |  |  | 
                            
                       
                          
                            
                                    | visakh16Very Important crosS Applying yaK Herder
 
 
                                    52326 Posts | 
                                        
                                          |  Posted - 2013-08-28 : 03:29:07 
 |  
                                          | quote:see some practical uses of apply herehttp://visakhm.blogspot.in/2010/01/multipurpose-apply-operator.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogsOriginally posted by Smokey
 Hey Stepson, that nailed it, thanks heaps.I knew Top would come into it but I don't think I've seen Cross Apply before, I've got some study to do.Thanks again.Mark
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | stepsonAged Yak Warrior
 
 
                                    545 Posts | 
                                        
                                          |  Posted - 2013-08-28 : 03:35:59 
 |  
                                          | with welcomeSCe-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mutsabinWeb |  
                                          |  |  | 
                            
                       
                          
                            
                                    | SmokeyStarting Member
 
 
                                    3 Posts | 
                                        
                                          |  Posted - 2013-08-28 : 19:21:27 
 |  
                                          | quote:Thanks Visakh, I'll check it out.Markhttp://visakhm.blogspot.in/2010/01/multipurpose-apply-operator.html
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | visakh16Very Important crosS Applying yaK Herder
 
 
                                    52326 Posts | 
                                        
                                          |  Posted - 2013-08-29 : 03:52:42 
 |  
                                          | quote:you're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogsOriginally posted by Smokey
 
 quote:Thanks Visakh, I'll check it out.Markhttp://visakhm.blogspot.in/2010/01/multipurpose-apply-operator.html
 
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | marcusn25Yak Posting Veteran
 
 
                                    56 Posts | 
                                        
                                          |  Posted - 2013-08-31 : 20:36:22 
 |  
                                          | I hope this helps.. Not tested.select 	 B.OrderNum	,B.orderDate	,B.Customer	,B.Modified	,B.ModifiedBy	,B.StatusFROM(Select	Row_Number () Over (Partition by OrderNum Order By OrderDate Desc) as LatestDate        H.OrderNum	,H.orderDate	,H.Customer	S.Modified,	S.ModifiedBy,	S.StatusFROM    OrderHeaderTable as  H         Inner Join        OrderStatusTable as S on 	H.OrderNum=S.OrderNumWHERE	S.Status='Confirm'	order by Modified desc)AS BWHEREB.LatestDate = 1M. Ncube |  
                                          |  |  | 
                            
                       
                          
                            
                                    | marcusn25Yak Posting Veteran
 
 
                                    56 Posts | 
                                        
                                          |  Posted - 2013-08-31 : 20:40:09 
 |  
                                          | [quote]Originally posted by marcusn25 I hope this helps.. Not tested.select 	 B.OrderNum	,B.orderDate	,B.Customer	,B.Modified	,B.ModifiedBy	,B.StatusFROM(Select	Row_Number () Over (Partition by OrderNum Order By OrderDate Desc) as LatestDate        H.OrderNum	,H.orderDate	,H.Customer	S.Modified,	S.ModifiedBy,	S.StatusFROM    OrderHeaderTable as  H         Inner Join        OrderStatusTable as S on 	H.OrderNum=S.OrderNumWHERE	S.Status='Confirm'	order by Modified desc)AS BWHEREB.LatestDate = 1M. NcubeM. Ncube
 |  
                                          |  |  | 
                            
                            
                                |  |