# K query to fetch &quot;last row for each ric&quot; from a table.

**URL:** <https://forum.kx.com/t/k-query-to-fetch-quot-last-row-for-each-ric-quot-from-a-table/7039>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [February 28, 2011, 12:35pm UTC](https://forum.kx.com/t/k-query-to-fetch-quot-last-row-for-each-ric-quot-from-a-table/7039 "2011-02-28T12:35:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akash8](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@Akash8](https://forum.kx.com/u/Akash8)\
**Post date:** [February 28, 2011, 12:35pm UTC](https://forum.kx.com/t/k-query-to-fetch-quot-last-row-for-each-ric-quot-from-a-table/7039/1 "2011-02-28T12:35:00Z")

</div>

Could anybody please help with the above queryEx:-Sample TableRic a b cL.T 1 2 3M.T 4 5 6L.T 1 4 6N.T 7 8 9N.T 9 1 2Sample OutputRic a b cL.T 1 4 6M.T 4 5 6N.T 9 1 2

---

<div class="post-metadata">

**Author:** ![Siddharth1](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@Siddharth1](https://forum.kx.com/u/Siddharth1)\
**Post date:** [February 28, 2011, 12:57pm UTC](https://forum.kx.com/t/k-query-to-fetch-quot-last-row-for-each-ric-quot-from-a-table/7039/2 "2011-02-28T12:57:00Z")

</div>

Try using

select last Ric, a, b,c by Ric from SampleTable

---

<div class="post-metadata">

**Author:** ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)\
**Post date:** [February 28, 2011, 1:10pm UTC](https://forum.kx.com/t/k-query-to-fetch-quot-last-row-for-each-ric-quot-from-a-table/7039/3 "2011-02-28T13:10:00Z")

</div>

the general form is “select from t where i=(last;i)fby Ric”

in this particular case, since you’re pulling the whole row, a shortcut form is “select by Ric from t”

btw, it’s easier to answer questions if you post your sample data in an executable format, e.g.

q)t:(Ric:`L.T`M.T`L.T`N.T`N.T;a:1 4 1 7 9;b:2 6 4 8 1;c:3 6 6 9 2)

---

<div class="post-metadata">

**Author:** ![Akash8](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@Akash8](https://forum.kx.com/u/Akash8)\
**Post date:** [March 1, 2011, 6:46am UTC](https://forum.kx.com/t/k-query-to-fetch-quot-last-row-for-each-ric-quot-from-a-table/7039/4 "2011-03-01T06:46:00Z")

</div>

this doesnt seem to be working.Could ou please explain it furtherhow does it return last row for each ric?On Feb 28, 6:10?pm, Aaron Davies \<aaron.dav…\> wrote:\> the general form is “select from t where i=(last;i)fby Ric”\>\> in this particular case, since you’re pulling the whole row, a shortcut form is “select by Ric from t”\>\> btw, it’s easier to answer questions if you post your sample data in an executable format, e.g.\>\> q)t:(Ric:`L.T`M.T`L.T`N.T`N.T;a:1 4 1 7 9;b:2 6 4 8 1;c:3 6 6 9 2)\>\> On Feb 28, 2011, at 7:35 AM, akash wrote:\>\>\>\>\>\>\>\> \> Could anybody please help with the above query\>\> \> Ex:-\> \> Sample Table\> \> Ric ?a ?b ?c\> \> L.T ? 1 ?2 ?3\> \> M.T 4 ?5 ?6\> \> L.T ?1 ?4 ?6\> \> N.T ?7 ?8 ?9\> \> N.T ?9 1 ? 2\>\> \> Sample Output\>\> \> Ric ?a ?b ?c\> \> L.T ?1 ?4 ?6\> \> M.T ?4 ?5 ?6\> \> N.T ?9 ?1 ?2\>\> \> –\> \>

Submitted via Google Groups\</aaron.dav…\>

---

<div class="post-metadata">

**Author:** ![Akash8](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@Akash8](https://forum.kx.com/u/Akash8)\
**Post date:** [March 1, 2011, 6:46am UTC](https://forum.kx.com/t/k-query-to-fetch-quot-last-row-for-each-ric-quot-from-a-table/7039/5 "2011-03-01T06:46:00Z")

</div>

this doesnt seem to be working.Could ou please explain it furtherhow does it return last row for each ric?On Feb 28, 6:10?pm, Aaron Davies \<aaron.dav…\> wrote:\> the general form is “select from t where i=(last;i)fby Ric”\>\> in this particular case, since you’re pulling the whole row, a shortcut form is “select by Ric from t”\>\> btw, it’s easier to answer questions if you post your sample data in an executable format, e.g.\>\> q)t:(Ric:`L.T`M.T`L.T`N.T`N.T;a:1 4 1 7 9;b:2 6 4 8 1;c:3 6 6 9 2)\>\> On Feb 28, 2011, at 7:35 AM, akash wrote:\>\>\>\>\>\>\>\> \> Could anybody please help with the above query\>\> \> Ex:-\> \> Sample Table\> \> Ric ?a ?b ?c\> \> L.T ? 1 ?2 ?3\> \> M.T 4 ?5 ?6\> \> L.T ?1 ?4 ?6\> \> N.T ?7 ?8 ?9\> \> N.T ?9 1 ? 2\>\> \> Sample Output\>\> \> Ric ?a ?b ?c\> \> L.T ?1 ?4 ?6\> \> M.T ?4 ?5 ?6\> \> N.T ?9 ?1 ?2\>\> \> –\> \>

Submitted via Google Groups\</aaron.dav…\>

---

<div class="post-metadata">

**Author:** ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)\
**Post date:** [March 1, 2011, 7:12am UTC](https://forum.kx.com/t/k-query-to-fetch-quot-last-row-for-each-ric-quot-from-a-table/7039/6 "2011-03-01T07:12:00Z")

</div>

please post a copy-and-paste of your session, along with what you expected the results to be
