2014-03-27 97 views
0

我有一個從多個連接中檢索的數據集。我在我的語句中使用了SELECT DISTINCT,但仍然在結果集中看到重複項。下面是代碼:來自SQL查詢的重複項

SELECT DISTINCT Account 
, PayoffAmtDOL as 'Payoff Amount DOL' 
, PayoffAmtLOG as 'Payoff Amount LOG' 
, PayoffAmountLive as 'Payoff Amount Live' 
, [Difference] 
, PrincipalBalance as 'Principal Balance' 
, CreationDate as 'Date Entered System' 
, CACSState as 'CACS State at Entry' 
, PaymentsMade AS 'Payments Made' 
, TotalPaymentAmount as 'Total Payment Amount' 
, 'Liquidation Percentage' = CASE WHEN PayoffAmountLive = 0 THEN 1 
          WHEN ISNULL([Difference],0) = ISNULL(PayoffAmtDOL, 0) THEN 1 
          WHEN ISNULL([Difference],0) < 0 AND ISNULL(PayoffAmtDOL, 0) > 0 THEN 0 
          WHEN ISNULL([Difference],0) > 0 AND ISNULL(PayoffAmtDOL, 0) < 0 THEN 1 
          WHEN ISNULL([Difference],0) > ISNULL(PayoffAmtDOL, 0) THEN 1 
          WHEN [Difference] > 0 AND ISNULL(PayoffAmtDOL, 0) = 0 THEN 1 
          WHEN ISNULL(PayoffAmtDOL, 0) = 0 THEN 0 
          ELSE ISNULL([Difference],0)/ISNULL(PayoffAmtDOL, 0) END 
      , Cnt = 1 
FROM 
(
SELECT DISTINCT a.Account, 
     c.PayoffAmtDOL, 
     c.PayoffAmtLOG, 
     (ISNULL(c.PayoffAmtCACS, cacs.payoff_amt)) as 'PayoffAmountLive', 
     (ISNULL(c.PayoffAmtDOL, 0) - (ISNULL(c.PayoffAmtCACS , ISNULL(cacs.payoff_amt, 0)))) as 'Difference', 
     c.PrincipalBalance, 
     c.CreationDate, 
     c.CACSState, 
     (SELECT COUNT(PaymentID) 
     FROM tblATLPaymentInfo p 
     WHERE p.AccountID = a.AccountID 
      AND CONVERT(DATETIME, CONVERT(VARCHAR(10), p.CreationDate, 101)) >= '1/1/2014' 
      AND CONVERT(DATETIME, CONVERT(VARCHAR(10), p.CreationDate, 101)) <= '3/27/2014' 
     ) as 'PaymentsMade', 
     (SELECT SUM(PaymentAmount) 
     FROM tblATLPaymentInfo p 
     WHERE p.AccountID = a.AccountID 
      AND CONVERT(DATETIME, CONVERT(VARCHAR(10), p.CreationDate, 101)) >= '1/1/2014' 
      AND CONVERT(DATETIME, CONVERT(VARCHAR(10), p.CreationDate, 101)) <= '3/27/2014' 
     ) as 'TotalPaymentAmount' 

FROM tblATLAcctInfo a 
RIGHT JOIN tblATLClaimInfo c 
    ON c.AccountID = a.AccountID 
LEFT JOIN SCFLOKYDCMSQL03.CACS_DM.dbo.Cacs_Info cacs 
    ON cacs.Account = a.Account 
WHERE CONVERT(DATETIME, CONVERT(VARCHAR(10), c.CreationDate, 101)) >= '1/1/2014' 
    AND CONVERT(DATETIME, CONVERT(VARCHAR(10), c.CreationDate, 101)) <= '3/27/2014' 
    AND c.ClaimTypeID = (SELECT DISTINCT ClaimTypeID FROM tblATLClaimType WHERE ClaimType = 'N02 - Claims') 
) a 
ORDER BY Account 

這裏是重複的行的一個示例:

AccountID DateEntered 
123  01/19/2014 
123  01/21/2014 
345  02/1/2014 
345  02/10/2014 

之間的差別似乎被輸入的日期。也許選擇Row_Number()然後刪除後面的日期可能是一個解決方案

+0

因爲你加入了多個表,所以你可能在每一行中都沒有相同的數據,你可以檢查行並告訴我你有沒有重複的行? –

+0

我看到一個具有相同的信息,除了創建日期 –

+1

如果你想區分只有一些列,你可以改變選擇和包括ROW_NUMBER()OVER(分區BY XXXX ORDER BY YYYY),然後選擇那些row_numbers等於1 –

回答

1

DISTINCT不應該返回多行..至少應該有一列在每一行中不同,否?對於字符數據,有時可能會被不可見的差異愚弄,例如尾隨空格。雖然不確定這是否是這種情況。

你可以舉一個重複行的例子嗎?

好的,我看到你的編輯。您必須選擇要顯示的日期。試試這個每個帳戶ID獲得的最早日期:

SELECT AccountID, MIN(DateEntered) AS DateEntered 
FROM .... 
GROUP BY AccountID 
ORDER BY AccountID 

您可以在選擇添加更多的列,只要它們是不同的,你不會得到更多的行。

如果需要,可以將COUNT(*)添加到select以獲取分組的行數。

+0

我加入到我的問題編輯上面 –

+0

我添加到我的答案上面,希望它有助於:) – SAS

1

DISTINCT只會拒絕重複的行,DateEntered在每個ID上都不相同。如果你想要最新的,使用Max(DateEntered)