Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>there a ton of different blog post about this topic out there.</p> <p>The most valuable for me, possibly was</p> <p>Xaprb post (see section "Select the top N rows from each group" for a detailed explaination)</p> <p><a href="http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/" rel="nofollow">http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/</a></p> <p>In your case that would mean. Modelling this SQL to your needs.</p> <pre><code>select type, variety, price from fruits where ( select count(*) from fruits as f where f.type = fruits.type and f.price &lt; fruits.price ) &lt;= 2; </code></pre> <p>lets try it out</p> <pre><code>SELECT r.price, r.room_id, h.hotel_id, h.city_id FROM Hotel h INNER JOIN Room r ON h.hotel_id = r.hotel_id WHERE ( select count(*) from Room as rt where r.hotel_id = rt.hotel_id and r.price &gt; rt.price ) &lt;= 10; ORDER BY h.city_id,h.hotel_id,r.price </code></pre> <p>as you can see you make an subquery to select the count of all rooms that have a price that is lower than the current root and see if the count is less than or equal to 10. </p> <p>Observe in particular how i changed the </p> <pre><code>f.price &lt; fruits.price </code></pre> <p>to </p> <pre><code>r.price &gt; rt.price </code></pre> <p>because in the example of Xaprb he selects the N cheapest fruits</p> <p>Hope that helps.</p> <p>BTW Stackoverflow isn't for solving homework. You should learn something your self as well ;-)</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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