在右外连接上返回结果在空表上与condtion一起

时间:2015-08-11 19:37:51

标签: tsql conditional-statements outer-join

所以我试图从billing_monthly_verizon_charges_detail_archive返回12个月的数据,如果没有数据而没有删除条件verizon.ITEM_DESCRIPTION <> 'Payment Received',我就无法找到返回0的方法。我明白为什么它没有返回任何数据,但是如何使用条件为每个月invoice_date table返回0 - 这是有数据时所需的。

SELECT 'H. Verizon' AS category, CONVERT(VARCHAR(7), id.INVOICE_DATE, 111) AS invoice_date, ISNULL(SUM(verizon.COST), 0.00) AS total_charges
FROM BILLING_MONTHLY_VERIZON_CHARGES_DETAIL_ARCHIVE AS verizon 
RIGHT OUTER JOIN
INVOICE_DATES AS id ON verizon.BILL_CYCLE_DATE = id.INVOICE_DATE
WHERE(id.INVOICE_DATE BETWEEN @BillingMonthMinus13 AND @BillingMonth) AND (verizon.ITEM_DESCRIPTION <> 'Payment Received')
GROUP BY id.INVOICE_DATE

2 个答案:

答案 0 :(得分:0)

COALESCE(verizon.ITEM_DESCRIPTION,'') <> 'Payment Received'

答案 1 :(得分:0)

考虑:

SELECT  
    'H. Verizon' AS category, 
    CONVERT(VARCHAR(7), INVOICE_DATE,111) AS invoice_date, 
    ISNULL(SUM(COST), 0.00) AS total_charges
FROM 
    (SELECT 
        INVOICE_DATE
    FROM
        INVOICE_DATES 
    WHERE
        INVOICE_DATE BETWEEN @BillingMonthMinus13 AND @BillingMonth ) id    LEFT OUTER JOIN
    (SELECT
        BILL_CYCLE_DATE, 
        COST
    FROM
        BILLING_MONTHLY_VERIZON_CHARGES_DETAIL_ARCHIVE
    WHERE
        ITEM_DESCRIPTION <> 'Payment Received'  ) verizon   ON 
    BILL_CYCLE_DATE = INVOICE_DATE
GROUP BY 
    INVOICE_DATE