内联表上的LEFT JOIN

时间:2010-11-16 17:51:09

标签: sql sql-server-2005

谷歌在这里没有帮助我,也没有微软在线帮助。

我有作为子查询生成的内联表。 (参见下面的简化代码,我还有几个内联表。)

现在,我有数据的地方很好。但是,有些情况下我需要在没有数据时返回结果。例如,内联表1返回我的活跃客户数...如果我指定一个没有活跃客户的范围,我将得不到整个查询的结果。

这是由于我的加入(AND IL1.transaction_id = th.transaction_id)

如何离开加入内联表?

我在il1.transaction_id = th.transaction_id上尝试了LEFT JOIN IL1,但它表示该表不存在。

select  SUM(th.total_net_retail_central) as 'Net Purchases  TY', 
            IL1.Active as 'Number of Active Customers TY',
            COUNT(th.transaction_id) as 'Number of Transactions TY'

FROM        

(SELECT transaction_type, COUNT(DISTINCT customer_id) as 'Active' from transaction_header  
where transaction_date BETWEEN @Active and @ToDate group by transaction_type)IL1, 
transaction_header th

INNER JOIN transaction_type tt ON th.transaction_type = tt.transaction_type
WHERE 
th.transaction_date Between @FromDate AND @ToDate

AND         IL1.transaction_type = th.transaction_type

GROUP BY 
            tt.transaction_type_description, IL1.Active

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

由于您正在运行SQL Server 2005,我将使用CTE来清理它。

;with cteIL1 as (
    SELECT transaction_type, COUNT(DISTINCT customer_id) as 'Active' 
        from transaction_header  
        where transaction_date BETWEEN @Active and @ToDate 
        group by transaction_type
)
select  SUM(th.total_net_retail_central) as 'Net Purchases  TY', 
        ac.Active as 'Number of Active Customers TY',
        COUNT(th.transaction_id) as 'Number of Transactions TY'
    FROM transaction_header th
        INNER JOIN transaction_type tt 
            ON th.transaction_type = tt.transaction_type
        LEFT JOIN cteIL1 IL1
            on th.transaction_type = IL1.transaction_type
    WHERE th.transaction_date Between @FromDate AND @ToDate
    GROUP BY tt.transaction_type_description, IL1.Active     

编辑:评论中提到的2000年非CTE版本:

select  SUM(th.total_net_retail_central) as 'Net Purchases  TY', 
        ac.Active as 'Number of Active Customers TY',
        COUNT(th.transaction_id) as 'Number of Transactions TY'
    FROM transaction_header th
        INNER JOIN transaction_type tt 
            ON th.transaction_type = tt.transaction_type
        LEFT JOIN (SELECT transaction_type, COUNT(DISTINCT customer_id) as 'Active' 
                       from transaction_header  
                       where transaction_date BETWEEN @Active and @ToDate 
                       group by transaction_type
                   ) IL1
            on th.transaction_type = IL1.transaction_type
    WHERE th.transaction_date Between @FromDate AND @ToDate
    GROUP BY tt.transaction_type_description, IL1.Active       

答案 1 :(得分:2)

您不应混合隐式和显式联接,您可能会得到不一致的结果。坦率地说,你永远不应该使用隐式连接。

看看这是否适合你:

SELECT  SUM(th.total_net_retail_central) AS 'Net Purchases  TY',  
            COALESCE(IL1.Active, 0) AS 'Number of Active Customers TY', 
            COUNT(th.transaction_id) AS 'Number of Transactions TY' 

FROM     transaction_header th      
INNER JOIN transaction_type tt 
    ON th.transaction_type = tt.transaction_type 
LEFT JOIN (SELECT transaction_type, COUNT(DISTINCT customer_id) AS 'Active' 
            FROM transaction_header   
            WHERE transaction_date BETWEEN @Active and @ToDate 
            GROUP BY transaction_type)IL1
    ON  IL1.transaction_type = th.transaction_type 
WHERE  th.transaction_date BETWEEN @FromDate AND @ToDate 
GROUP BY  tt.transaction_type_description, COALESCE(IL1.Active, 0)

答案 2 :(得分:1)

select  SUM(th.total_net_retail_central) as 'Net Purchases  TY', 
            IL1.Active as 'Number of Active Customers TY',
            COUNT(th.transaction_id) as 'Number of Transactions TY'
FROM        
(SELECT transaction_type, 
       COUNT(DISTINCT customer_id) as 'Active' 
         from transaction_header  
    where transaction_date BETWEEN @Active and @ToDate 
     group by transaction_type ) IL1
     right join
                transaction_header th
                 on IL1.transaction_type = th.transaction_type
INNER JOIN transaction_type tt 
            ON th.transaction_type = tt.transaction_type
WHERE 
th.transaction_date Between @FromDate AND @ToDate
GROUP BY 
            tt.transaction_type_description, IL1.Active

如果我正确阅读此内容,我相信您需要在IL1和TH之间正确加入

IL1          正确加入                     transaction_header th                      在IL1.transaction_type = th.transaction_type