| 
                
Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | ccbuilderStarting Member
 
 
                                        17 Posts | 
                                            
                                            |  Posted - 2013-06-04 : 15:38:40 
 |  
                                            | I have three tablesRequest[RID] pk int auto,[Description] varchar(200) not null,Request_Dates[DateID] pk int auto,[Date] smalldatetime not null,[RID] fk int not nullDetails[RowID] pk int auto,[DateID] fk int not null,[NumericalOrder] tinyint not null,[Address_From] varchar(100) not null,[Address_To] varchar(100) not nullI also have such data from the following query SELECT Request_Dates.DateID, Request_Dates.[Date],        Details.RowID, NumericalOrder, Address_From, Address_ToFROM Request_Dates INNER JOIN Details ON      Request_Dates.DateID = Details.DateIDWHERE Request_Dates.RID=1ORDER BY [Date], NumericalOrder DateID  Date        RowID  NumericalOrder  Address_From       Address_To1       2013-06-01  1      1               123 Elm Street     457 Langley Ave1       2013-06-01  13     2                                  263 Oak St1       2013-06-01  58     3                                  1004 Pine Rd108     2013-06-04  581    1               457 W. St Francis  800 E Montana108     2013-06-04  590    2                                  1515 N 16th StThe query I would like help with is one where the address_to from the previous row is the Address_From for the next rowIn this example, the first row will have no prior address_to, but the second row would have to read: Address_From: 457 Langley Ave --- Address_To: 263 Oak RdThe RowID column items are not sequential because users delete rows from their data entry.  What is sequential for each date is the NumericalOrder column.  A given date can have many NumericalOrders which always begin with 1Your assistance is mostly appreciated. |  |  
                                    | James KMaster Smack Fu Yak Hacker
 
 
                                    3873 Posts | 
                                        
                                          |  Posted - 2013-06-04 : 16:49:32 
 |  
                                          | quote:Originally posted by ccbuilder
 I have three tablesRequest[RID] pk int auto,[Description] varchar(200) not null,Request_Dates[DateID] pk int auto,[Date] smalldatetime not null,[RID] fk int not nullDetails[RowID] pk int auto,[DateID] fk int not null,[NumericalOrder] tinyint not null,[Address_From] varchar(100) not null,[Address_To] varchar(100) not nullI also have such data from the following query
 SELECT Request_Dates.DateID, Request_Dates.[Date],        Details.RowID, NumericalOrder, Address_From, Address_ToFROM Request_Dates INNER JOIN Details ON      Request_Dates.DateID = Details.DateIDWHERE Request_Dates.RID=1ORDER BY [Date], NumericalOrder DateID  Date        RowID  NumericalOrder  Address_From       Address_To1       2013-06-01  1      1               123 Elm Street     457 Langley Ave1       2013-06-01  13     2                                  263 Oak St1       2013-06-01  58     3                                  1004 Pine Rd108     2013-06-04  581    1               457 W. St Francis  800 E Montana108     2013-06-04  590    2                                  1515 N 16th StThe query I would like help with is one where the address_to from the previous row is the Address_From for the next rowIn this example, the first row will have no prior address_to, but the second row would have to read: Address_From: 457 Langley Ave --- Address_To: 263 Oak RdThe RowID column items are not sequential because users delete rows from their data entry.  What is sequential for each date is the NumericalOrder column.  A given date can have many NumericalOrders which always begin with 1Your assistance is mostly appreciated. 
 SELECT	r.DateID,	r.[Date],	d.RowID,	d.NumericalOrder,	COALESCE(NULLIF(d.Address_From,''),dp.Address_To) AS Address_From,	d.AddressToFROM	Request_Dates r	INNER JOIN Details d ON d.DateId = r.DateID	LEFT JOIN Details dp ON dp.DateId = d.DateId AND dp.RowId = d.RowID-1ORDER BY	r.Date, d.NumericalOrder; |  
                                          |  |  |  
                                    | ccbuilderStarting Member
 
 
                                    17 Posts | 
                                        
                                          |  Posted - 2013-06-04 : 17:11:17 
 |  
                                          | Thanks for the prompt reply.The query works when RowID is sequential like in the case of DateID=108; however, with DateID=1, I get NULL for Address_FromUsers can delete and insert rows from other instances so RowID cannot be expected to be sequential.Thanks in advance |  
                                          |  |  |  
                                    | James KMaster Smack Fu Yak Hacker
 
 
                                    3873 Posts | 
                                        
                                          |  Posted - 2013-06-04 : 17:23:59 
 |  
                                          | quote:Ah, I had missed that - sorry about that.Originally posted by ccbuilder
 Thanks for the prompt reply.The query works when RowID is sequential like in the case of DateID=108; however, with DateID=1, I get NULL for Address_FromUsers can delete and insert rows from other instances so RowID cannot be expected to be sequential.Thanks in advance
 
 ;WITH cte AS(	SELECT *,		ROW_NUMBER() OVER (PARTITION BY DateId ORDER BY RowId) AS NewRowId	FROM		Details)SELECT	r.DateID,	r.[Date],	d.RowID,	d.NumericalOrder,	COALESCE(NULLIF(d.Address_From,''),dp.Address_To) AS Address_From,	d.AddressToFROM	Request_Dates r	INNER JOIN cte d ON d.DateId = r.DateID	LEFT JOIN cte dp ON dp.DateId = d.DateId AND dp.NewRowId = d.NewRowID-1ORDER BY	r.Date, d.NumericalOrder; |  
                                          |  |  |  
                                    | ccbuilderStarting Member
 
 
                                    17 Posts | 
                                        
                                          |  Posted - 2013-06-04 : 18:00:41 
 |  
                                          | Awesome, it works.Thank you so much James :) |  
                                          |  |  |  
                                |  |  |  |  |  |