Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You might use this query - the purpose is to get [n] resaleids for each shipwynum, where [n=2] is </p> <pre><code>SELECT (SUBSTRING_INDEX(GROUP_CONCAT(CONVERT(resaleid, CHAR(8)) ORDER BY resaleid DESC), ',', 2)) AS ID FROM resale WHERE deleted = 'N' GROUP BY shipwynum </code></pre> <p>Please not that GROUP_CONCAT result is truncated to max len 1024 (group_concat_max_len). And this might have performance issue so please feel free to update and optimize as needed.</p> <p>Other suggestion would be to set a rank but not how will work this for you. You can check rank : <a href="http://www.oracle-base.com/articles/misc/rank-dense-rank-first-last-analytic-functions.php#rank" rel="nofollow">http://www.oracle-base.com/articles/misc/rank-dense-rank-first-last-analytic-functions.php#rank</a></p> <p>Hope this helps.</p> <pre><code>SELECT re.recdat, re.resaleid, IF (re.benownsal != '', owsal.ownshortnam, if ((re.grptypsal != '' &amp;&amp; re.bogrpidsal != ''), if (bosal.bogrpshort != '', bosal.bogrpshort, bosal.bogrpnam), cou1.nation)) as seller, IF (re.benownpur != '', owpur.ownshortnam, if ((re.grptyppur != '' &amp;&amp; re.bogrpidpur != ''), if (bopur.bogrpshort != '', bopur.bogrpshort, bopur.bogrpnam), cou.nation)) as buyer, re.benownpur, re.grptyppur, re.bogrpidpur, re.statuscod, re.showinob, re.benownsal, re.grptypsal, re.bogrpidsal FROM resale AS re left join owner as owpur on owpur.ownwynum = re.benownpur left join owner as owsal on owsal.ownwynum = re.benownsal left join bogroup as bopur on bopur.bogrpid = re.bogrpidpur left join bogroup as bosal on bosal.bogrpid = re.bogrpidsal left join country as cou on cou.coucod = re.buynation left join country as cou1 on cou1.coucod = re.selnation WHERE LOCATE (re.resaleid, ( SELECT GROUP_CONCAT(it.resaleid) AS ids FROM ( SELECT (SUBSTRING_INDEX(GROUP_CONCAT(CONVERT(resaleid, CHAR(8)) ORDER BY resaleid DESC), ',', 2)) AS ID FROM resale WHERE deleted = 'N' GROUP BY shipwynum ) it )) &lt;&gt; 0 /* AND re.deleted = 'N' */ order by re.saltyp desc, re.recdat </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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