Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 消除重复项。。而且更有趣_Sql_Sql Server - Fatal编程技术网

Sql 消除重复项。。而且更有趣

Sql 消除重复项。。而且更有趣,sql,sql-server,Sql,Sql Server,我的查询目标是计算每个单独贷款标识符的原始贷款余额。但是,我使用的数据多次使用每个贷款标识符来显示不同月份的当前实际贷款余额。因此,当我试图计算原始贷款余额时,它会在每次贷款标识符出现时添加原始贷款余额。我只想为每个贷款标识符隔离一个原始贷款余额,但我在这样做时遇到困难。我最初的想法是使用where子句对贷款的独特特性进行过滤。例如,仅过滤一个月报告期的数据。但是,月度报告期从“2011年第四季度绩效”开始“在筛选“总收购文件”中的“原始未付本金余额”时,不能将数据添加为where子句。”。我已

我的查询目标是计算每个单独贷款标识符的原始贷款余额。但是,我使用的数据多次使用每个贷款标识符来显示不同月份的当前实际贷款余额。因此,当我试图计算原始贷款余额时,它会在每次贷款标识符出现时添加原始贷款余额。我只想为每个贷款标识符隔离一个原始贷款余额,但我在这样做时遇到困难。我最初的想法是使用where子句对贷款的独特特性进行过滤。例如,仅过滤一个月报告期的数据。但是,月度报告期从“2011年第四季度绩效”开始“在筛选“总收购文件”中的“原始未付本金余额”时,不能将数据添加为where子句。”。我已尝试加入这两个表,但在尝试筛选搜索时遇到问题。有人知道如何消除列表中的重复项,并且只计算每个贷款标识符的一个“原始未付本金余额”吗?谢谢你的帮助,如果你需要我澄清,请告诉我。我的代码发布在下面,带有无法绑定的“where”子句

SQL Server 2012

 SELECT All a.[Loan Identifier]
          ,[Monthly Reporting Period]
          ,[Servicer Name]
          ,[Current Interest Rate]
          ,[Current Actual Unpaid Principal Balance]  
          ,[Loan Age]
          ,[Remaining Months to Legal Maturity]
          ,[Adjusted Remaining Months to Maturity]
          ,[Maturity Date]
          ,b.[ORIGINAL UNPAID PRINCIPAL BALANCE (UPB)]
          ,[Zero Balance Code]
          ,[Zero Balance Effective Date]

            From dbo.Performance_2011Q4 a
      Join dbo.TotalAcquisition b On a.[Loan Identifier] = b. [Loan Identifier]


      Select (sum(convert (float, (dbo.[TotalAcquisition].[ORIGINAL UNPAID PRINCIPAL BALANCE (UPB)])))) from dbo.TotalAcquisition 

 Where dbo.Performance_2011Q4.[Monthly Reporting Period] = '03/01/2013'

在样本数据方面,你们并没有给我们提供太多的信息,所以我对你们的数据做了一些假设。我的假设是,尽管您在TotalAcquisition中有多个记录,但对于给定的贷款标识符,原始未付本金余额始终相同。如果是这样的话,像这样的事情应该会起作用

SELECT DISTINCT [Loan Identifier], [ORIGINAL UNPAID PRINCIPAL BALANCE (UPB)] FROM TotalAcquisition

如果这不是您想要的,请为我们提供更多信息,例如每个表中一个加载id的样本行。

您没有为我们提供多少样本数据,因此我对您的数据做一些假设。我的假设是,尽管您在TotalAcquisition中有多个记录,但对于给定的贷款标识符,原始未付本金余额始终相同。如果是这样的话,像这样的事情应该会起作用

SELECT DISTINCT [Loan Identifier], [ORIGINAL UNPAID PRINCIPAL BALANCE (UPB)] FROM TotalAcquisition

如果这不是您要查找的,请提供更多信息,例如每个表中一个加载id的样本行。

如果您只想从查询中消除重复行,您可以使用

命令。

如果您只想从查询中删除重复的行,您可以使用

命令。

在Where子句中使用子查询来筛选每个贷款标识符的所有记录,但最早的记录除外

Select * From dbo.Performance_2011Q4 a
   Join dbo.TotalAcquisition b 
       On a.[Loan Identifier] = b. [Loan Identifier]
Where  [Monthly Reporting Period] = 
   (Select Min([Monthly Reporting Period])
    From dbo.Performance_2011Q4 
    Where [Loan Identifier] = a.[Loan Identifier])

在Where子句中使用子查询来筛选每个贷款标识符的所有记录,但最早的记录除外

Select * From dbo.Performance_2011Q4 a
   Join dbo.TotalAcquisition b 
       On a.[Loan Identifier] = b. [Loan Identifier]
Where  [Monthly Reporting Period] = 
   (Select Min([Monthly Reporting Period])
    From dbo.Performance_2011Q4 
    Where [Loan Identifier] = a.[Loan Identifier])

您可以使用一个简单的CTE来消除重复,只需对查询进行非常小的更改

WITH cte AS (
    SELECT DISTINCT [Loan Identifier], [ORIGINAL UNPAID PRINCIPAL BALANCE (UPB)]
    FROM dbo.TotalAcquisition
)
SELECT All a.[Loan Identifier]
         ,[Monthly Reporting Period]
         ,[Servicer Name]
         ,[Current Interest Rate]
         ,[Current Actual Unpaid Principal Balance]  
         ,[Loan Age]
         ,[Remaining Months to Legal Maturity]
         ,[Adjusted Remaining Months to Maturity]
         ,[Maturity Date]
         ,b.[ORIGINAL UNPAID PRINCIPAL BALANCE (UPB)]
         ,[Zero Balance Code]
         ,[Zero Balance Effective Date]
FROM dbo.Performance_2011Q4 a
JOIN cte b 
  ON a.[Loan Identifier] = b.[Loan Identifier]

您可以使用一个简单的CTE来消除重复,只需对查询进行非常小的更改

WITH cte AS (
    SELECT DISTINCT [Loan Identifier], [ORIGINAL UNPAID PRINCIPAL BALANCE (UPB)]
    FROM dbo.TotalAcquisition
)
SELECT All a.[Loan Identifier]
         ,[Monthly Reporting Period]
         ,[Servicer Name]
         ,[Current Interest Rate]
         ,[Current Actual Unpaid Principal Balance]  
         ,[Loan Age]
         ,[Remaining Months to Legal Maturity]
         ,[Adjusted Remaining Months to Maturity]
         ,[Maturity Date]
         ,b.[ORIGINAL UNPAID PRINCIPAL BALANCE (UPB)]
         ,[Zero Balance Code]
         ,[Zero Balance Effective Date]
FROM dbo.Performance_2011Q4 a
JOIN cte b 
  ON a.[Loan Identifier] = b.[Loan Identifier]

是的,谢谢你的澄清。对于给定的贷款标识,原始未付本金余额是相同的,感谢您的澄清。给定贷款标识符的原始未付本金余额相同