如何实现左外连接?

时间:2014-04-10 04:23:39

标签: sql sql-server join

我有这个查询我想实现左外连接我该怎么做

SELECT A.cl_id, A.cl_name, A.number_of_orders, B.Dispatch
FROM (
    SELECT CL_Clients.cl_id, CL_Clients.cl_name,COUNT(*) AS number_of_orders
    FROM CL_Clients,CLOI_ClientOrderItems
    WHERE CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
    GROUP BY CL_Clients.cl_name,CL_Clients.cl_id
    ) A
, (
    SELECT CL_Clients.cl_id,count(*) AS Dispatch
    FROM CLOI_ClientOrderItems,CL_Clients
    WHERE cloi_current_status = '12'  
        AND CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
    GROUP BY CL_Clients.cl_name,CL_Clients.cl_id, CLOI_ClientOrderItems.cloi_current_status
    ) B

WHERE A.cl_id = B.cl_id 

预期产出

Cl_id    | Cl_name       |Builtorder |Dispatch
------------------------------------------------
admin    |admin 1    |0      |20
agar     |Agar Murdoch   |102        |1 
Alexander|Alexander James|33         |65    
alexandra|Alexandra West |64         |14    
Archer   |Archer & Co    |0      |5
asbank   |Stirling       |80         |8 
asnapebr |Andrew Snape   |183        |1 
asnapech |Andrew Snape Ch|213        |0 

2 个答案:

答案 0 :(得分:3)

我认为您的完整查询可以写成几行,请尝试下面的代码。

第一种方式,单一加入就是全部!!!

Select a.cl_id
        ,a.cl_name
        ,count(b.cl_id) as number_of_orders
        ,Sum(Case When b.cloi_current_status ='12' 
                           --and Condition1 and condition2
                     Then 1 else 0 end) as Dispatch
    from CL_Clients as a
    left join CLOI_ClientOrderItems as b on a.cl_id = b.cl_id
 group by a.cl_id
        ,a.cl_name

第二种方式,多个连接

Select a.cl_id
        ,a.cl_name
        ,Isnull(orderTable.orders,0) as number_of_orders
        ,ISNULL(dTable.Dispatch,0) as Dispatch
    from CL_Clients as a
    outer apply 
        (
            select b.cl_id,count(b.cl_id) as orders from  CLOI_ClientOrderItems as b 
            where a.cl_id = b.cl_id 
            group by b.cl_id
        )as orderTable 
    outer apply
        (
            select d.cl_id,count(*) as Dispatch from CLOI_ClientOrderItems as d
            where a.cl_id = d.cl_id
                and d.cloi_current_status ='12' 
                --and condition2
                --and condition3
                --..............
                --and condition250
            group by a.cl_id
        )as dTable

在上述声明中, 第一次加入orderTable用于获取订单数量。 第二次加入dTableis用于获取已分派的订单数量,它可以包含您要求的多个条件,-----Condition2 .....是虚拟文本,您可以删除所有评论,例如Condition2,Condition3 .. 。&把你的实际情况。

答案 1 :(得分:1)

这就是你在TSQL中的表现,但@ AK47有解决你更大问题的解决方案。

SELECT A.cl_id, A.cl_name, A.number_of_orders, B.Dispatch
FROM (
    SELECT CL_Clients.cl_id, CL_Clients.cl_name,COUNT(*) AS number_of_orders
    FROM CL_Clients,CLOI_ClientOrderItems
    WHERE CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
    GROUP BY CL_Clients.cl_name,CL_Clients.cl_id
    ) A
LEFT OUTER JOIN (
    SELECT CL_Clients.cl_id,count(*) AS Dispatch
    FROM CLOI_ClientOrderItems,CL_Clients
    WHERE cloi_current_status = '12'  
        AND CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
    GROUP BY CL_Clients.cl_name,CL_Clients.cl_id, CLOI_ClientOrderItems.cloi_current_status
    ) B
ON A.cl_id = B.cl_id