2010-01-07 3 views
1

J'ai une requête pour soustraire le solde actuel d'une table avec le solde précédent d'un autre tableau d'historique. Quand un produit particulier n'a pas de solde actuel mais a un solde précédent, je peux le soustraire correctement ... dans ce cas, il sera comme 0 - 100.Problème avec la jointure externe complète ne fonctionnant pas comme prévu

Cependant, lorsque le produit a un solde actuel mais aucun solde précédent , Je suis incapable d'obtenir le résultat. Ma requête ne sélectionne même pas la balance courante même si j'ai fait une jointure externe complète sur les deux tables.

Suite est ma requête:

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 
+0

pourriez-vous précis dans vos balises ce SGBD utilisez-vous? – chburd

+0

Ressemblances à MS SQL Server. –

+0

Oui en utilisant MS SQL Server 2005 – Nicholas

Répondre

0

Merci pour l'aide mais je ne pouvais pas faire fonctionner la façon dont je voulais utiliser les réponses ci-dessous. Finalement, j'ai décidé d'aller jusqu'au bout et de déclarer deux tables temporaires pour tenir les soldes actuels et précédents. Je pense que je veux rester aussi loin de jointures externes que possible; p

code

est ci-dessous:

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 
2

Vous regardez un attribut de la table HIST dans la clause WHERE. S'il n'y a pas d'entrée dans la table HIST, la clause ne correspond pas et rejette donc la ligne.

Remplacer

WHERE HIST.TransactionDate = @PreviousDate 

avec

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

Il est à cause de:

WHERE HIST.TransactionDate = @PreviousDate 

Cette force Hist.TransactionDate ne pas être nulle.

Vous pouvez utiliser

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

ou changer la jonction à:

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 
Questions connexes