试图加入两个sql语句

时间:2014-06-27 13:39:14

标签: sql join

我想在TripId上加入查询1和查询2。

查询1

SELECT tblTrips.TripId,tblVehicles.VehicleNo FROM tblTrips INNER JOIN tblVehicles ON tblTrips.VehicleId = tblVehicles.VehicleId

查询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

第一个查询的输出是TripId和VehicleNo的列表。 第二个查询的输出是TripId,Amount和description的列表。

我的愿望输出是TripId,VehicleNo,数量和描述。

2 个答案:

答案 0 :(得分:1)

WITH (公用表格式)的语法允许您创建多个CTE。

使用它可以将Query2的最后一部分转换为CTE (我将命名为Query2),您对Query1的查询也可以转入CTE (我将其命名为Query1)

然后,最终的SELECT声明可以简单地将这两个CTE加在一起。

;

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
      )
,
  Query2 AS (
  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
  )
,
  Query1 AS (
  <Your Code For Query1>
)

SELECT
  *
FROM
  Query1
INNER JOIN
  Query2
    ON Query1.TripID = Query2.TripID

我无法检查您的查询,因为您使用的布局不具备可读性。

答案 1 :(得分:0)

使用CTE合并查询(没有更改/检查您的代码,只是为了便于阅读而格式化它 - 输入非常可怕)

;WITH T1 AS (
    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
)
, Q1 AS (
    SELECT T1.TripId
         , SUM(T1.Amount) AS Amount
         , STUFF((
                SELECT '#', '    ' + CONVERT(VARCHAR(MAX), T2.DeductionFor) 
                FROM T1 AS T2 
                WHERE T1.TripId = T2.TripId FOR XML PATH(''))
            ,1,1,'') AS [Description] 
    FROM T1
    GROUP BY TripId
)
, Q2 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 * 
FROM Q1 
INNER JOIN Q2 ON Q1.TripId = Q2.TripId