1

我正在使用以下 SQL 语句,如果没有任何值因此“合并”,我希望它返回空值,但我有一个问题 - 它不起作用:)

我想可能是因为该行不存在,所以我将如何将该条件添加到以下语句中?

SELECT 
  D1.dr, 
  D1.cr, 
  D1.asset_no, 
  (open_bal + dr - cr) AS closing_balance 
FROM (SELECT 
        COALESCE(SUM(dr_amount), 0) AS dr, 
        COALESCE(SUM(cr_amount), 0) AS cr, 
        asset_no 
      FROM posting, sysasset 
      WHERE posting.asset_no = @AssetNumber 
        AND period >= asset_open_per 
      GROUP BY asset_no) AS D1, asset 
WHERE D1.asset_no = asset.asset_no
4

2 回答 2

1

哦,我认为记录没有显示,因为其中一些不存在于另一张桌子上,对吧?未显示的原因是因为您正在使用 加入他们INNER JOIN,请尝试使用LEFT OUTER JOIN.

SELECT  D1.dr, 
        D1.cr, 
        D1.asset_no, 
        (COALESCE(open_bal,0) + dr - cr) AS closing_balance 
FROM 
    (
        SELECT  COALESCE(SUM(dr_amount), 0) AS dr, 
                COALESCE(SUM(cr_amount), 0) AS cr, 
                asset_no 
        FROM    posting
                LEFT JOIN sysasset 
                    ON  posting.asset_no = asset.asset_no
        WHERE   posting.asset_no = @AssetNumber  AND 
                period >= asset_open_per 
        GROUP   BY asset_no
    ) AS D1
    LEFT JOIN asset 
        ON D1.asset_no = asset.asset_no
于 2013-01-29T12:17:07.330 回答
1

尝试用 isnull 替换合并。具有合并的表达式仍可被视为空值。MSDN 参考

于 2013-01-29T12:18:26.740 回答