Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL finding second largest value in a table
    primarykey
    data
    text
    <p>Tbl1 looks like: </p> <pre><code> CUSIP_ID1 CUSIP_ID2 cor dt_pnts 00768Y818 00162Q726 0.974691 252 00768Y818 00162Q205 0.874761 4 00768Y818 00162Q103 0.774691 48 73935X153 00162Q726 0.979131 39 73935X153 132061201 0.975207 252 73935X153 34416W866 0.967654 152 739371102 464287671 0.937278 252 739371102 464287309 0.935797 252 78467V103 33939L407 0.951472 35 78467V103 78463X541 0.930144 252 78467V103 57060U795 0.923911 108 </code></pre> <p>My code is: (tbl3 is just a reference table for the ticker) </p> <pre><code>insert into tbl2 (ticker, cusip_id, maxcor, dt_pnts) select b.Ticker, a.CUSIP_ID1 No_indx_cusip, max(abs(a.cor)) maxcor, dt.dt_pnts from tbl1 a inner join tbl3 b on a.CUSIP_ID1 = b.CUSIP_ID and a.dt_pnts &gt; 20 inner join ( select cusip_id1, cor, dt_Pnts from tbl1 ) dt ON a.CUSIP_ID1 = dt.CUSIP_ID1 group by a.CUSIP_ID1, b.Ticker, dt.dt_pnts, dt.cor having abs(dt.cor) = MAX(abs(a.cor)) select * from tbl2 </code></pre> <p>it just finds the maximum correlation value of each ticker/cusip_id with its respective date points to return:</p> <pre><code>ticker cusip_id maxcor dt_pnts TTFS 00768Y818 0.974691 252 PXLG 739371102 0.937278 252 INKM 78435X153 0.979131 39 RLY 78467V103 0.951472 35 </code></pre> <p>However, I would like to find the value of the 2nd largest correlation (cor) given the same condition (that dt_pnts is > 20) for each CUSIP_ID1. I tried messing around with <code>dense_rank()</code> a little but I'm still a beginner so i need help (please!)</p> <p>return would be: </p> <pre><code>ticker cusip_id maxcor dt_pnts TTFS 00768Y818 0.774681 48 PXLG 739371102 0.935797 252 INKM 78435X153 0.975207 252 RLY 78467V103 0.923911 108 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload