Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

sql join

$
0
0

HI,

How to join these two statement on TripId. you don't need to  understand the query .

statement 1

----------------------------------------------

SELECT   tblTrips.TripId,tblTrips.DestinationDistrictId, tblTrips.VehicleId, tblTrips.No,tblVehicles.VehicleNo, tblTrips.CoachNo,CONVERT(VARCHAR(24),tblTrips.GoDate,105) as GoDate, tblTrips.GoTime,  
CASE WHEN tblTrips.IsCome=1 THEN CONVERT(VARCHAR(24),tblTrips.ComeDate,105) ELSE '-' END AS ComeDate,
CASE WHEN tblTrips.IsCome=1 THEN tblTrips.ComeTime ELSE '-' END AS ComeTime,
CASE WHEN tblTrips.IsCome=1 THEN (SD.DistrictName+' - '+DD.DistrictName+' - '+SD.DistrictName) ELSE (SD.DistrictName+' - '+DD.DistrictName) END as Destination, tblSupervisors.Name AS Supervisor, tblDrivers.Name AS Driver, tblTrips.AdvanceAmount, tblTrips.AdvanceDescription
FROM   tblTrips INNER JOIN
                         tblSupervisors ON tblTrips.SuperVisorId = tblSupervisors.SupervisorId INNER JOIN
                         tblDrivers ON tblTrips.DriverId = tblDrivers.DriverId INNER JOIN
                         tblDistricts SD ON tblTrips.StartDistrictId = SD.DistrictId  INNER JOIN
                         tblDistricts  DD ON tblTrips.DestinationDistrictId = DD.DistrictId  INNER JOIN
                         tblVehicles ON tblTrips.VehicleId = tblVehicles.VehicleId

statement 2

----------------------------------------------

;with T1 as (
SELECT        tblTrips.TripId, tblTripDeductions.Amount, CONVERT(VARCHAR(400),tblDeductionTypes.DeductionType+' - '+tblTripDeductions.Description+' - '+ CONVERT(VARCHAR(24),tblTripDeductions.Amount)) as DeductionFor
FROM            tblTrips INNER JOIN
                         tblTripDeductions ON tblTrips.TripId = tblTripDeductions.TripId INNER JOIN
                         tblDeductionTypes ON tblTripDeductions.DeductionId = tblDeductionTypes.DeductionId
      )select T1.TripId, SUM(T1.Amount) as Amount, stuff((select '#','    ' + CONVERT(varchar(1000),T2.DeductionFor) from T1 AS T2 where T1.TripId = T2.TripId for xml path('')),1,1,'') [Description] from  T1
Group by TripId



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>