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  | 
                             
                            
                                    | 
                                         milo1981 
                                        Starting Member 
                                         
                                        
                                        18 Posts  | 
                                        
                                        
                                            
                                            
                                             Posted - 2013-11-03 : 07:42:13
                                            
  | 
                                             
                                            
                                            | I have a table (Vehicles) which contains a list of vehicles.VehicleIDPlateNoCurrentDriverI also have a table (History) which contains a the driver history for the vehicles:HistoryIDVehicleIDReceivedDate (vehicle receiving date)DriverNameI have another table (Repairs) which contains the repairs for all the vehicles:RepairIDVehicleIDRepairDateRepairCostUsing SQL Server and based on the History table, I want to get all the RepairCost values between two dates for a given DriverName.For example, I want to get all the RepairCost values for driver 'John Doe', between 01.01.2013 and 01.05.2013, who was allocated to three different vehicles in that period.My query, so far: [url]sqlfiddle.com/#!3/fcebf/3[/url] | 
                                             
                                         
                                     | 
                             
       
                            
                       
                          
                            
                                    | 
                                     visakh16 
                                    Very Important crosS Applying yaK Herder 
                                     
                                    
                                    52326 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2013-11-03 : 09:15:27
                                          
  | 
                                         
                                        
                                          Sounds like this to meDECLARE @Driver varchar(50),@StartDt datetime, @EndDt datetimeSELECT @Driver = 'John Doe',@StartDt = '20130101' ,@EndDt = '20130501';With VehicleAllocationAS(SELECT h.*,h1.ChangeDateFROM History hOUTER APPLY (SELECT MIN(ReceivedDate) AS ChangeDate             FROM History             WHERE VehicleID = h.VehicleID             AND DriverName <> h.DriverName             AND ReceivedDate  > h.ReceivedDate             )h1WHERE h.DriverName = @Driver)SELECT *FROM VehicleAllocation hINNER JOIN Repairs rON r.VehicleID = h.VehicleIDWHERE DriverName = @DriverAND RepairDate > = @StartDtAND RepairDate < @EndDt + 1AND RepairDate BETWEEN h.ReceivedDate AND COALESCE(h.ChangeDate,RepairDate) EDIT : Fixed typo------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     milo1981 
                                    Starting Member 
                                     
                                    
                                    18 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2013-11-03 : 11:17:37
                                          
  | 
                                         
                                        
                                          | Thank you Visakh, for your reply!But, I get an error (Incorrect syntax near the keyword 'FROM') at this line:FROM VehicleAllocation hDoes it have to do with the fact that VehicleAllocation alias is the same as History alias (h)?Later Edit: I get it. It was missing a '*' in the SELECT before the FROM where the error was appearing.  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     visakh16 
                                    Very Important crosS Applying yaK Herder 
                                     
                                    
                                    52326 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2013-11-03 : 13:19:15
                                          
  | 
                                         
                                        
                                          quote: Originally posted by milo1981 Thank you Visakh, for your reply!But, I get an error (Incorrect syntax near the keyword 'FROM') at this line:FROM VehicleAllocation hDoes it have to do with the fact that VehicleAllocation alias is the same as History alias (h)?Later Edit: I get it. It was missing a '*' in the SELECT before the FROM where the error was appearing.
  sorry tht was a typo. Fixed in posted queryDid it gave you expected result?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     milo1981 
                                    Starting Member 
                                     
                                    
                                    18 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2013-11-27 : 07:19:16
                                          
  | 
                                         
                                        
                                          | Visakh,What if I want to get the SUM of different colums from different tables?For example, SUM(Total) column from 'Repairs' table, SUM(Value) column from 'Tires' table...How can I adapt the script?EDIT:I discoverd a problem with the line 'AND DriverName <> h.DriverName'. Why is that line useful? If I had the same driver name, one after the other, in the History table, it skipped to the last car delivery date for that driver name.Sample data:'History' tableReceivedDate	DriverName04.11.2013	Mike13.11.2013	Dan15.11.2013	Dan17.11.2013	Ryan20.11.2013	Dan22.11.2013	Ryan25.11.2013	Mike26.11.2013	Dan29.11.2013	Ryan04.12.2013	Dan'Repairs' tableRepairDate	RepairCost05.11.2013	2615.3014.11.2013	135.6616.11.2013	4913.0418.11.2013	538.9221.11.2013	152.4823.11.2013	5946.8926.11.2013	3697.6427.11.2013	734.0130.11.2013	279.62Query resultRepairDate	RepairCost07.11.2013	380.0014.11.2013	135.6616.11.2013	4913.0416.11.2013	4913.0421.11.2013	152.4827.11.2013	734.01As you can see in the query result, line 3 and 4 have the same value/date.The query interval was 01-01-2013 <-> 31-12-2013.Thanks!  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     visakh16 
                                    Very Important crosS Applying yaK Herder 
                                     
                                    
                                    52326 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2013-11-27 : 11:40:10
                                          
  | 
                                         
                                        
                                          | Thats required as attempt is to find date till vehicle  was with particular driver.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     milo1981 
                                    Starting Member 
                                     
                                    
                                    18 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2013-11-27 : 12:31:25
                                          
  | 
                                         
                                        
                                          | Is that line required? If I remove it, the duplicate line disappears.  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |   
     
     
            
              
	     |  
		
			
     
          
		 |