简体   繁体   English

INNER JOIN可以用同一个表中的另一个值替换列的值 - 对于所有行

[英]Can INNER JOIN replace a column's value with another value from the same table - for all rows

I need to write a query to replace one of the column's values with a value from another row in the same table. 我需要编写一个查询来将列中的一个值替换为同一个表中另一行的值。 I tried to use a simple inner join, but I'm not seeing the results I need. 我试图使用一个简单的内连接,但我没有看到我需要的结果。

Here's an example of the table BANK_LOANS: 这是BANK_LOANS表的一个例子:

MONTH_ID   LOAN_NUMBER    BANK_NAME       AMOUNT
========   ===========    ==========      ========
76         00-100         Bank One        100.00
75         00-100         The Bank One    150.00
74         00-100         Bank 1          150.00
76         00-200         Another Bank    300.00 
75         00-200         Another Bank    500.00

The data is loaded from outside sources which doesn't verify the bank name, so the spelling for a bank name for a given loan number can possibly vary. 数据是从外部来源加载的,不会验证银行名称,因此给定贷款号码的银行名称的拼写可能会有所不同。 The customer wants whatever BANK_NAME is in the database for the max MONTH_ID to be used for all the results for that LOAN_NUMBER, and they want the results sorted by BANK_NAME. 客户希望BANK_NAME在数据库中的任何内容,最大MONTH_ID用于该LOAN_NUMBER的所有结果,并且他们希望结果按BANK_NAME排序。 So for instance, loan 00-100 needs a BANK_NAME of "Bank One" for all rows. 因此,例如,贷款00-100需要所有行的BANK_NAME为“Bank One”。

I tried using an inner join to do this, but wasn't getting the correct results 我尝试使用内部联接来执行此操作,但未获得正确的结果

SELECT name.BANK_NAME, 
       bl.LOAN_NUMBER,
       bl.AMOUNT
FROM BANK_LOANS    
INNER JOIN BANK_LOANS home
    ON name.LOAN_NUMBER = bl.LOAN_NUMBER
    AND name.MONTH_ID = 67  --the max id provided to the query
ORDER BY name.BANK_NAME, bl.LOAN_NUMBER, bl.MONTH_ID DESC

I think I have it working with an sub-query, but it's kind of ugly. 我想我有一个子查询,但它有点难看。 I was wondering if there was a better way (best practices) of accomplishing this with joins or other oracle functions. 我想知道是否有更好的方法(最佳实践)通过连接或其他oracle函数来实现这一点。

This seems to work, but it just feels wrong: 这似乎有效,但它只是错了:

SELECT name.BANK_NAME, 
       bl.LOAN_NUMBER,
       bl.AMOUNT
FROM BANK_LOANS, 
     (SELECT bl2.BANK_NAME, bl2.LOAN_NUMBER
      FROM BANK_LOANS
      WHERE bl2.MONTH_ID = 76  --max month id provided to query
     ) name
WHERE name.LOAN_NUMBER = bl.LOAN_NUMBER
ORDER BY name.BANK_NAME, bl.LOAN_NUMBER, bl.MONTH_ID DESC
with
     test_data ( month_id, loan_number, bank_name, amount ) as (
       select 76, '00-100', 'Bank One'    , 100.00 from dual union all
       select 75, '00-100', 'The Bank One', 150.00 from dual union all
       select 74, '00-100', 'Bank 1'      , 150.00 from dual union all
       select 76, '00-200', 'Another Bank', 300.00 from dual union all
       select 75, '00-200', 'Another Bank', 500.00 from dual
     )
-- end of test data (not part of the solution).
-- SQL query begins BELOW THIS LINE; use with your table.
select month_id, loan_number, 
       first_value(bank_name) 
              over (partition by loan_number order by month_id desc) as bank_name,
       amount
from   test_data
;

MONTH_ID LOAN_N BANK_NAME    AMOUNT
-------- ------ ------------ ------
      74 00-100 Bank One        150
      75 00-100 Bank One        150
      76 00-100 Bank One        100
      75 00-200 Another Bank    500
      76 00-200 Another Bank    300

5 rows selected.

Use analytic functions: 使用分析函数:

select bl.loannumber, bl.amount,
       max(bank_name) keep (dense_rank first order by month_id desc) over (partition by loannumber) as bank_name
from bank_loans bl;
SELECT month_id,
   loan_number,
   MIN(bank_name) over(PARTITION BY loan_number ORDER BY month_id DESC),
   amount
FROM bank_loans;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM