如何捕获前20个单位

时间:2014-07-17 15:46:57

标签: sql

(MS SQL)我正在尝试编写一个查询来捕获源自新办公室的前20笔贷款。我办公室的贷款号码是9位数(100100001)。前4位数字是办公室标识符,其余5位数是发起的贷款的总计。因此,如果贷款文件的贷款编号为100800015,这意味着它来自办公室1008,并且是该办公室发起的第15个文件。

目前我有以下查询:

SELECT    
    l.FundedDate,  
    l.LoanNumber,  
    l.BorrowerLastName,  
    l.LoanType  

FROM  
    Loan.LoanQA AS l  

WHERE  
    l.FundedDate BETWEEN '6/01/2014' AND '6/30/2014'  
    AND l.LoanType = 'FHA'

如何调整上述内容,以便能够在特定时间段(即月份)内从任何办公室捕获前20个单位?因此,前四个字符的含义会因办公室而异。

我不想调整WHERE子句中的前4位数(分支ID)。我只想输入一个日期范围,如果有新的贷款(最后5位数),我希望它能够捕获前20个单位。

3 个答案:

答案 0 :(得分:0)

查询取决于您使用的数据库引擎,但通常您将使用子字符串函数来确定贷款办公室,然后包含where子句以匹配您正在寻找的办公室:

SELECT TOP 20                                            -- only the first 20
       l.FundedDate,
       l.LoanNumber,
       l.BorrowerLastName,
       l.LoanType
 FROM
       Loan.LoanQA AS l

WHERE
       l.FundedDate BETWEEN '6/01/2014' AND '6/30/2014'
   AND l.LoanType = 'FHA'
   AND SUBSTRING(l.LoanNumber, 1, 4) = '1001'               -- only office 1001
ORDER BY l.FundedDate

以上查询将针对MS SQL Server。

如果您使用的是其他数据库引擎,则可能必须使用其他内容来限制行。例如,对于MySQL,您使用LIMIT 1, 20

答案 1 :(得分:0)

由于您没有指定DBMS,这是我能帮到的最好的。

SQL Server: 如果您的DBMS是SQL Server,那么您可以通过使用TOP这样做:

SELECT    TOP 20
          l.FundedDate,  
          l.LoanNumber,  
          l.BorrowerLastName,  
          l.LoanType  

FROM      Loan.LoanQA AS l  

WHERE     l.FundedDate BETWEEN '6/01/2014' AND '6/30/2014'  
AND       l.LoanType = 'FHA'
AND       SUBSTRING(CONVERT(NVARCHAR(10),l.LoanNumber), 1, 4) = '1234'
ORDER BY  l.FundedDate

MySQL: 如果您的DBMS是MySQL,那么您可以通过使用LIMIT这样做:

SELECT    l.FundedDate,  
          l.LoanNumber,  
          l.BorrowerLastName,  
          l.LoanType  

FROM      Loan.LoanQA AS l  

WHERE     l.FundedDate BETWEEN '6/01/2014' AND '6/30/2014'  
AND       l.LoanType = 'FHA'
AND       SUBSTRING(Convert(l.LoanNumber, char(10)), 1, 4) = '1234'
ORDER BY  l.FundedDate
LIMIT     20

Oracle

如果您的DBMS是Oracle,那么您可以通过使用ROWNUM这样做:

SELECT    l.FundedDate,  
          l.LoanNumber,  
          l.BorrowerLastName,  
          l.LoanType  

FROM      Loan.LoanQA AS l  

WHERE     l.FundedDate BETWEEN '6/01/2014' AND '6/30/2014'  
AND       l.LoanType = 'FHA'
AND       ROWNUM <= 20

希望这有帮助!!!

答案 2 :(得分:-1)

我能够回答我自己的问题,下面是MS SQL SMS中的查询。我确信那里的某个人能够同意WHERE子句。

SELECT      
    l.FundedDate,  
    l.LoanNumber,  
    l.BorrowerLastName,  
    l.LoanType

FROM  
    Loan.LoanQA AS l  

WHERE  
    l.FundedDate BETWEEN '1/01/2014' AND '6/30/2014'  
    AND(l.LoanNumber LIKE '%00001'    
    OR l.LoanNumber LIKE '%00002'  
    OR l.LoanNumber LIKE '%00003'  
    OR l.LoanNumber LIKE '%00004'  
    OR l.LoanNumber LIKE '%00005'  
    OR l.LoanNumber LIKE '%00006'  
    OR l.LoanNumber LIKE '%00007'  
    OR l.LoanNumber LIKE '%00008'  
    OR l.LoanNumber LIKE '%00009'  
    OR l.LoanNumber LIKE '%00010'  
    OR l.LoanNumber LIKE '%00011'  
    OR l.LoanNumber LIKE '%00012'  
    OR l.LoanNumber LIKE '%00013'  
    OR l.LoanNumber LIKE '%00014'  
    OR l.LoanNumber LIKE '%00015'  
    OR l.LoanNumber LIKE '%00016'  
    OR l.LoanNumber LIKE '%00017'  
    OR l.LoanNumber LIKE '%00018'  
    OR l.LoanNumber LIKE '%00019'  
    OR l.LoanNumber LIKE '%00020')  

ORDER BY  
    l.BranchID