Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This should work (not sure with the origin of your columns though):</p> <pre><code>WITH cte AS (SELECT position, price, orderid, buylog = Min(buylog) OVER( partition BY position,orderid), otherlog = Max(otherlog) OVER( partition BY position,orderid), rn = Row_number() OVER( partition BY position,orderid ORDER BY position) FROM T1) SELECT position, price, orderid, buylog, otherlog FROM cte WHERE rn = 1 </code></pre> <p>Here's a <a href="http://www.sqlfiddle.com/#!3/37eff/4/0" rel="nofollow"><strong>DEMO</strong></a></p> <p><strong>Edit</strong> So here is the complete query with your joins, to take your edited question into account:</p> <pre><code>WITH cte AS (SELECT olt.position, ot.price, ot.orderid, ot.buylog = Min(ot.buylog) OVER ( partition BY olt.position, ot.orderid), olt.otherlog = Max(olt.otherlog) OVER ( partition BY olt.position, ot.orderid), rn=Row_number() OVER( partition BY olt.position ORDER BY olt.position) FROM ordertable AS ot INNER JOIN anothertable AS at ON ordertable.userid = anothertable.userid INNER JOIN otherlogtable AS olt ON anothertable.id = otherlogtable.sessionlogid) SELECT position, price, orderid, buylog, otherlog FROM cte WHERE rn = 1 </code></pre> <p>The problem was:</p> <ol> <li>remove the table names from the outer select on the <code>CTE</code> (e.g. <code>SELECT position</code> instead of <code>SELECT olt.position</code>) </li> <li>remove the comma between the <code>PARTITION BY</code> part of the <code>ROW_NUMBER</code> and the <code>ORDER BY</code>: <code>rn=Row_number() OVER( partition BY olt.position ORDER BY olt.position)</code></li> </ol>
    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.
    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