简体   繁体   English

将对应的日期添加到最大值

[英]Adding a corresponding date to max value

The query that I have written below is giving me everything that I need except for the corresponding High_Water date. 我在下面编写的查询为我提供了我所需的一切,除了相应的High_Water日期。 Basically what this query is doing is simple. 基本上,此查询的作用很简单。 Its giving me the MAX value throughout the life of the symbol however I'm not sure how to write in to the query so that the result set will show a 5th column with the corresponding date of the MAX value! 它在整个符号生命期内为我提供了MAX值,但是我不确定如何写入查询,以便结果集将显示第5列以及相应的MAX值日期!

For example if a symbol X was entered in to the database on 01/01/2015 and it contains data up until today (11/06/2015). 例如,如果符号X在2015年1月1日被输入到数据库中,并且其中包含直到今天(2015年6月11日)的数据。 My current query will find the MAX AUM for symbol X and that max value was on 06/01/2015. 我当前的查询将找到符号X的MAX AUM,最大值为2015年6月1日。 I need my query to show me the 06/01/2015 date. 我需要查询才能向我显示2015年6月1日的日期。 Basically I need the query to find the MAX value and its corresponding date so that I know on what date the MAX AUM is reflected on. 基本上,我需要查询以找到MAX值及其对应的日期,以便我知道MAX AUM反映在哪个日期。

SELECT
    A.SMBL,
    B.MLTPL,
    BEGINNING_DATE,
    ROUND(max(AUM/1000000.00)) AS HIGH_WATER_AUM      
FROM
    TBL1 A 
    JOIN TBL2 B ON A.SMBL = B.SMBL
WHERE
    A.SMBL IN = 'X' 
GROUP BY
    A.SMBL, B.MLTPL, BEGINNING_DATE
ORDER BY
    SMBL

Sounds like you need KEEP FIRST/LAST: 听起来您需要先保持/后保持:

select   a.smbl,
         b.mltpl,
         beginning_date,
         round(max(aum / 1000000.00)) as high_water_aum,
         max(high_water_date) keep (dense_rank first order by aum desc) hw_dt_of_max_row
from     tbl1 a 
         inner join tbl2 b on (a.smbl = b.smbl)
where    a.smbl = 'X'
group by a.smbl,
         b.mltpl,
         beginning_date
order by smbl;

The keep (dense_rank first order by aum desc) bit is doing the ordering and picking the row(s) with the highest aum. keep (dense_rank first order by aum desc)排序为keep (dense_rank first order by aum desc)位是对aum最高的行进行排序和选择。 (You could rewrite this as keep (dense_rank last order by aum) if you wanted to.) (如果需要,您可以将其重写为keep (dense_rank last order by aum) 。)

The max(high_water_date) is there in case there's more than one row that has the highest aum value - it simply picks the latest high_water_date to display. 如果有多行具有最高的aum值,则存在max(high_water_date) -它只是选择要显示的最新high_water_date。 You could change that to min(high_water_date) if you so wished. 如果愿意,可以将其更改为min(high_water_date)


Here's a simple example illustrating the principle: 这是说明原理的简单示例:

with sample_data as (select 1 id, 20 val1 from dual union all
                     select 2 id, 10 val1 from dual union all
                     select 3 id, 40 val1 from dual union all
                     select 4 id, 100 val1 from dual union all
                     select 5 id, 70 val1 from dual union all
                     select 6 id, 100 val1 from dual union all
                     select 7 id, 80 val1 from dual union all
                     select 8 id, 70 val1 from dual union all
                     select 9 id, 90 val1 from dual)
select max(val1) max_val1,
       max(id) keep (dense_rank first order by val1 desc) max_val1_max_id1,
       max(id) keep (dense_rank last order by val1) max_val1_max_id2
from   sample_data;

  MAX_VAL1 MAX_VAL1_MAX_ID1 MAX_VAL1_MAX_ID2
---------- ---------------- ----------------
       100                6                6

Just create a row_number to select the highest row. 只需创建row_number以选择最高的行。

SELECT *
FROM 
    (
      SELECT A.SMBL, 
             B.MLTPL, 
             BEGINNING_DATE, 
             ROUND(AUM/1000000.00) AS HIGH_WATER_AUM,
             A.nav_date,      
             ROW_NUMBER() over 
                (PARTITION BY A.SMBL, B.MLTPL, BEGINNING_DATE ORDER BY AUM DESC) AS RN
      FROM TBL1 A 
      JOIN TBL2 B 
        ON A.SMBL = B.SMBL
      WHERE A.SMBL IN ('X','Y','Z')
    ) t
WHERE RN = 1
ORDER BY SMBL

Here is the query that I was looking to write. 这是我想要编写的查询。 I gives me the exact result set that I was looking for. 我给了我想要的确切结果集。

SELECT A.SMBL,
       B.MLTPL,
       BEGINNING_DATE,
       ROUND(MAX(c.AUM / 1000000.00)) AS HIGH_WATER_AUM,
       max(a.nav_date) AS HIGH_WATER_AUM
FROM TBL1 A
INNER JOIN TBL2 B
        ON A.SMBL = B.SMBL
INNER JOIN (
           SELECT SMBL,
                  max(AUM) AS AUM
           FROM TBL1
           GROUP BY symbol
           ) c
        ON A.SMBL = C.SMBL
       AND c.AUM = a.AUM
WHERE A.SMBL IN ('X','Y','Z')
GROUP BY A.SMBL,
         B.MLTPL,
         BEGINNING_DATE
ORDER BY SMBL

@JuanCarlosOropeza. @JuanCarlosOropeza。 The result set that I was looking for is this 我一直在寻找的结果集是

SMBL    MLTPL   BEGINNING_DATE       MAX(A.NAV_DATE)      HIGH_WATER_AUM
A        10    2008-12-01 00:00:00   2011-05-02 00:00:00         100
B        10    2011-10-04 00:00:00   2013-11-27 00:00:00         600
X        10    2008-11-24 00:00:00   2009-06-17 00:00:00         300
Y        10    2008-11-24 00:00:00   2015-03-26 00:00:00         500
Z        10    2008-12-01 00:00:00   2011-09-02 00:00:00         700

The result set that your query gave me was 您的查询给我的结果集是

SMBL     MLTPL  BEGINNING_DATE       MAX(A.NAV_DATE)       HIGH_WATER_AUM
A        10    2008-12-01 00:00:00   2015-10-15 00:00:00         100
B        10    2011-10-04 00:00:00   2015-10-15 00:00:00         600
X        10    2008-11-24 00:00:00   2015-10-15 00:00:00         300
Y        10    2008-11-24 00:00:00   2015-10-15 00:00:00         500
Z        10    2008-12-01 00:00:00   2015-10-15 00:00:00         700

You can see how its simply assigning a date to all of the different symbols even though their HIGHT_WATER_AUM in reality was on different dates. 您可以看到它如何简单地为所有不同的符号分配日期,即使实际上它们的HIGHT_WATER_AUM在不同的日期也是如此。

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

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