English 中文(简体)
Problem with Full Outer Join not working as expected
原标题:

I have a query to subtract current balance from one table with previous balance from another history table. When a particular product has no current balance but has previous balance, I am able to subtract it correctly...in this case it will be like 0 - 100.

However, when the product has current balance but no previous balance, I am unable to get the result. My query does not even select the current balance even though I have done a full outer join on both tables.

Following is my query:

SELECT  DATEPART(yyyy, @ExecuteDate) * 10000 + DATEPART(mm, @ExecuteDate) * 100 + DATEPART(dd, @ExecuteDate) AS Period_Key,
                CASE WHEN GL.GL_Acct_Key IS NULL THEN 0 ELSE GL.GL_Acct_Key END AS GL_Acct_Key,
                CASE WHEN BANK.Bank_Type_Key IS NULL THEN 0 ELSE BANK.Bank_Type_Key END AS Bank_Type_Key,
                CASE WHEN TSC.TSC_Key IS NULL THEN 0 ELSE TSC.TSC_Key END AS TSC_Key,
                ISNULL(FT.CurrentBalance,0) - ISNULL(HIST.CurrentBalance,0) AS Actual_Income_Daily,
                CASE WHEN BR.Branch_Key IS NULL THEN 0 ELSE BR.Branch_Key END AS Branch_Key
        FROM    WSB_Stage.dbo.Stage_TS_Daily_Income_Hist HIST
                FULL OUTER JOIN WSB_Stage.dbo.Stage_TS_Daily_Income FT
                    ON FT.GLAcctID = HIST.GLAcctID AND
                       FT.BankType = HIST.BankType AND
                       FT.BranchNumber = HIST.BranchNumber
                LEFT OUTER JOIN WSB_Mart.dbo.Dim_Branch BR
                    ON HIST.BranchNumber = BR.Branch_Code
                LEFT OUTER JOIN WSB_Mart.dbo.Dim_GL_Acct GL
                    ON HIST.GLAcctID = GL.Acct_Code
                LEFT OUTER JOIN WSB_Mart.dbo.Dim_Bank_Type BANK
                    ON HIST.BankType = BANK.Bank_Type_Code
                LEFT OUTER JOIN WSB_Stage.dbo.Param_Branch_TSC_Map BRTSC
                    ON HIST.BranchNumber = BRTSC.BranchNumber
                LEFT OUTER JOIN WSB_Mart.dbo.Dim_TSC TSC
                    ON BRTSC.RegionCode = TSC.TSC_Code
        WHERE   HIST.TransactionDate = @PreviousDate
            AND GL.Acct_Type_Code =  Interest 
            AND BANK.Bank_Type_Key = 1
最佳回答

Thanks for the help but I couldn t get it to work the way I wanted using the below answers. Finally, I decided to go the long way and declare two temporary tables to hold current and previous balances. I think I want to stay as far away from outer joins as possible ;p

Code is below:

INSERT INTO @PreviousGL
    SELECT  GLAcctID,
            BankType,
            BranchNumber,
            ISNULL(CurrentBalance,0) AS Current_Balance
    FROM    WSB_Stage.dbo.Stage_TS_Daily_Income_Hist
    WHERE   TransactionDate = @PreviousDate

INSERT INTO @CurrentGL
SELECT  GLAcctID,
        BankType,
        BranchNumber,
        ISNULL(CurrentBalance,0) AS Current_Balance
FROM    WSB_Stage.dbo.Stage_TS_Daily_Income

INSERT INTO @DailyIncomeGL
SELECT  CASE WHEN CURR.GLAcctID IS NULL THEN PREV.GLAcctID
             WHEN PREV.GLAcctID IS NULL THEN CURR.GLAcctID
             WHEN CURR.GLAcctID IS NULL AND PREV.GLAcctID IS NULL THEN 0
             ELSE CURR.GLAcctID
        END AS GLAcctID,
        CASE WHEN CURR.BankType IS NULL THEN PREV.BankType
             WHEN PREV.BankType IS NULL THEN CURR.BankType
             WHEN CURR.BankType IS NULL AND PREV.BankType IS NULL THEN   
             ELSE CURR.BankType
        END AS BankType,
        CASE WHEN CURR.BranchNumber IS NULL THEN PREV.BranchNumber
             WHEN PREV.BranchNumber IS NULL THEN CURR.BranchNumber
             WHEN CURR.BranchNumber IS NULL AND PREV.BranchNumber IS NULL THEN 0
             ELSE CURR.BranchNumber
        END AS BranchNumber,
        ISNULL(CURR.CurrentBal,0) - ISNULL(PREV.CurrentBal,0) AS Actual_Income_Daily
FROM    @CurrentGL CURR
        FULL OUTER JOIN @PreviousGL PREV
            ON CURR.GLAcctID = PREV.GLAcctID AND
               CURR.BankType = PREV.BankType AND
               CURR.BranchNumber = PREV.BranchNumber
问题回答

You are checking a attribute of the HIST table in the WHERE clause. If there is no entry in the HIST table, the clause doesn t match and thus discards the row.

Replace

WHERE   HIST.TransactionDate = @PreviousDate

with

WHERE   (HIST.TransactionDate IS NULL OR HIST.TransactionDate = @PreviousDate)

It s because of:

WHERE HIST.TransactionDate = @PreviousDate

This forces Hist.TransactionDate not to be null.

You could use

WHERE (HIST.TransactionDate = @PreviousDate OR HIST.TransactionDate IS NULL)

or change the join to:

FULL OUTER JOIN WSB_Stage.dbo.Stage_TS_Daily_Income FT
    ON FT.GLAcctID = HIST.GLAcctID AND
    FT.BankType = HIST.BankType AND
    FT.BranchNumber = HIST.BranchNumber AND
    HIST.TransactionDate = @PreviousDate




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签