PostgreSQL : Get first and last inserted record for each ID column
<p>I have the following table with two columns.</p>

<p><strong>Table</strong>:</p>

<pre><code>create table tbl1
(
    p_id int,
    p_price int
);
</code></pre>

<p><strong>Insertion</strong>:</p>

<pre><code>INSERT INTO tbl1 values(1,100);
INSERT INTO tbl1 values(1,50);  
INSERT INTO tbl1 values(1,20);

INSERT INTO tbl1 values(2,10);  
INSERT INTO tbl1 values(2,20);

INSERT INTO tbl1 values(3,22);  
INSERT INTO tbl1 values(3,89);  
INSERT INTO tbl1 values(3,500);
</code></pre>

<p>Query: Following query gives me the row number of each row.</p>

<pre><code>SELECT p_id,p_price,row_number() over(partition by p_id order by p_id) rn
from tbl1
</code></pre>

<p>I want to get only first and last inserted record for each product id (p_id).</p>

<p><strong>Expected Result</strong>:</p>

<pre><code>p_id    p_price    
-----------------
1   100
1   20
2   10  
2   20
3   22
3   500 
</code></pre>