如何使用CTE添加左连接,检查我的查询

时间:2016-08-24 11:09:48

标签: sql-server sql-server-2008 sql-server-2008-r2

我有以下查询我想添加我的查询左连接与CTE如何这请帮助我因为我有驱动程序ID我想要第二个最后的驱动程序ID但我想添加左连接与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()over(通过Transdate desc按DriverID顺序划分)作为Rn, 将(1)结束(由DriverID分区)作为来自Fleet_DriverCommision的cnt) 选择AgentFeesTotal,Dri​​verId 来自cte 其中(Rn = 2且cnt> 1)或(Rn = 1且cnt = 1)

从Fleet_Driver t2中选择t2.DriverNo 离开加入 cte c 在c.DriverId = t2.Id

1 个答案:

答案 0 :(得分: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)