English 中文(简体)
页: 1 仅为最新记录总数
原标题:SQL Total of only the most recent record

I m试图获得一份总付者名单,2000年。 我只需要列入最近所附的“pymt_amt”,由该笔贷款中的每一笔贷款的顺序定出。 下面的法典几乎给我提供了我所需要的东西,但“行为总金额”除外;“2000年”包括每一张ched子_pymt_amt,而不只是最近的ched子_pymt_amt_eff_dt。 我如何把我的成果限制在只使用最近所附的“pymt_amt_eff_dt”的后面。

select 
    person_id, sum(sched_pymt_amt) as total_pay_amt
from
    (select 
         p.person_id, lp.loan_id, lp.sched_pymt_amt, lp.sched_pymt_amt_eff_dt, 
         row_number() over (partition by l.loan_id order by sched_pymt_amt_eff_dt desc) rn
     from
         person p 
     inner join
         loan_dtl l on p.person_id = l.brwr_person_id 
     inner join
         loan_repymt_plan lp ON l.loan_id = lp.loan_id
     where 
         1 = 1) a
where 
    rn = 1
having 
    sum(sched_pymt_amt) > 2000 --this is the problem line
group by 
    person_id
问题回答

我建议你将内部质问减少到只需要1张表格。 将<代码>rn=1作为该次舱的合并条件。 而且,通过条款将保留条款移至集团之后。 我还建议对贷款进行计算,因为一个人可能拥有超过1笔贷款。

SELECT
      p.person_id
    , sum(lp.sched_pymt_amt) AS total_pay_amt
    , count(distinct lp.loan_id) AS num_loans
FROM person p
INNER JOIN loan_dtl l ON p.person_id = l.brwr_person_id
INNER JOIN  (
            select
                  loan_id
                , sched_pymt_amt
                , row_number() OVER (
                    PARTITION BY loan_id 
                    ORDER BY sched_pymt_amt_eff_dt DESC
                    ) rn
            from loan_repymt_plan
        ) lp ON l.loan_id = lp.loan_id AND lp.rn = 1
-- where there is more then 0 to pay, details unknown
GROUP BY
      p.person_id
HAVING sum(lp.sched_pymt_amt) > 2000 --"having" occurs AFTER group by




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签