2016-08-24 74 views
0

我有以下查詢我想添加我的查詢左加入與CTE如何做到這一點,請幫助我,因爲我有驅動程序ID我想第二個最後的驅動程序ID,但我想添加左連接與CTE如何添加與CTE的左加入,檢查我的查詢

 select d.Id,d.DriverNo,d.DriverName,TransId=dc.Id,dc.FromDate,dc.ToDate,dc.IsPaid, 
     Active=(case when (dc.weekoff is null or dc.weekoff=0) then 'Active' else 'Off' end), 
     Rent=(case when (IsNull(dc.CommissionTotal,0))> IsNull(dc.AccJobsTotal,0) then IsNull(dc.CommissionTotal,0)-(IsNull(dc.AccJobsTotal,0)) else 0 end), 
     BalanceDue=IsNull(dc.OldBalance,0), 
     AgentCommission=IsNull(dc.AgentFeesTotal,0), 
     PDA= (case when (dc.weekoff is null or dc.weekoff=0) then (IsNull(dc.PDARent,0)+IsNull(dc.CollectionDeliveryCharges,0)) else 0 end), 
     Total=(case when (IsNull(dc.CommissionTotal,0))> IsNull(dc.AccJobsTotal,0) then IsNull(dc.CommissionTotal,0)-(IsNull(dc.AccJobsTotal,0)) else 0 end) 
     +((IsNull(dc.OldBalance,0)) 
     +((IsNull(dc.AgentFeesTotal,0))) 
     +(case when (dc.weekoff is null or dc.weekoff=0) then (IsNull(dc.PDARent,0)+IsNull(dc.CollectionDeliveryCharges,0)) else 0 end)) 
     from Fleet_Driver d 
     inner join Fleet_DriverCommision dc 
     on d.Id=dc.DriverId 
     where dc.Id in (select Max(Id) from Fleet_DriverCommision 
     group by DriverId) as T1 
     left join on 


> LEFT JOIN WITH CTE 


     With cte as 
     (select AgentFeesTotal,DriverId,Row_Number()over(Partition by DriverID order by Transdate desc) as Rn, 
     count(1)over(Partition by DriverID) as cnt from Fleet_DriverCommision) 
     Select AgentFeesTotal,DriverId 
     from cte 
     Where (Rn = 2 and cnt > 1) or (Rn = 1 and cnt = 1) 

這是示例

與CTE 如 (選擇AgentFeesTotal,Dri​​verId,ROW_NUMBER()以上(分區由DriverID爲了通過Transdate降序)用作Rn, 計數(1)在(由DriverID分區)作爲來自Fleet_DriverCommision的cnt) 選擇AgentFeesTo TAL,從CTE DriverId 凡(RN = 2和CNT> 1)或(RN = 1和CNT = 1)

選擇t2.DriverNo從Fleet_Driver T2 左加入 CTEÇ 上c.DriverId = t2.Id

+0

請提供一個最簡單的工作示例。 – buhtz

+0

與CTE 如 (選擇AgentFeesTotal,Dri​​verId,ROW_NUMBER()以上(分區由DriverID爲了通過Transdate降序)用作Rn, 計數(1)在(由DriverID分區),如從Fleet_DriverCommision CNT) 選擇AgentFeesTotal,Dri​​verId 從CTE 凡(RN = 2和CNT> 1)或(RN = 1和CNT = 1) 選擇t2.DriverNo從Fleet_Driver T2 左加入 CTEç 上c.DriverId = t2.Id –

回答

2

看起來你正在努力使用CTE的語法。 CTE聲明需要在查詢的其餘部分之前發生,然後像另一個表一樣行事。另請注意,WITH聲明必須是第一條語句或遵循分號。這應該讓你走上正軌。另外請務必檢查MSDN文檔中的示例。

--With statement first - must follow ; if there are multiple statements... 
    With cte as 
    (select AgentFeesTotal,DriverId, 
    Row_Number()over(Partition by DriverID order by Transdate desc) as Rn, 
    count(1)over(Partition by DriverID) as cnt 
    from Fleet_DriverCommision 
    ) 
    -- ...then select statement... 
    select d.Id,d.DriverNo,d.DriverName,TransId=dc.Id, 
    dc.FromDate,dc.ToDate,dc.IsPaid, 
    Active=(case when (dc.weekoff is null or dc.weekoff=0) then 'Active' else 'Off' end), 
    Rent=(case when (IsNull(dc.CommissionTotal,0))> IsNull(dc.AccJobsTotal,0) then IsNull(dc.CommissionTotal,0)-(IsNull(dc.AccJobsTotal,0)) else 0 end), 
    BalanceDue=IsNull(dc.OldBalance,0), 
    AgentCommission=IsNull(dc.AgentFeesTotal,0), 
    PDA= (case when (dc.weekoff is null or dc.weekoff=0) then (IsNull(dc.PDARent,0)+IsNull(dc.CollectionDeliveryCharges,0)) else 0 end), 
    Total=(case when (IsNull(dc.CommissionTotal,0))> IsNull(dc.AccJobsTotal,0) then IsNull(dc.CommissionTotal,0)-(IsNull(dc.AccJobsTotal,0)) else 0 end) 
    +((IsNull(dc.OldBalance,0)) 
    +((IsNull(dc.AgentFeesTotal,0))) 
    +(case when (dc.weekoff is null or dc.weekoff=0) then (IsNull(dc.PDARent,0)+IsNull(dc.CollectionDeliveryCharges,0)) else 0 end)) 
    from Fleet_Driver d 
    inner join Fleet_DriverCommision dc 
    on d.Id=dc.DriverId 
    --...join in cte as a normal table 
    left join cte 
    on --join criteria here 
    where dc.Id in (select Max(Id) from Fleet_DriverCommision 
    group by DriverId) as T1 

    --move the remainder of the logic into your query 
    Select AgentFeesTotal,DriverId 
    from cte 
    Where (Rn = 2 and cnt > 1) or (Rn = 1 and cnt = 1)