<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="136049" data-post-id="136049">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="snnowwy" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/snnowwy/120/16088_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  snnowwy
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="acrolink" data-post="1" data-topic="14337">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/acrolink/48/8985_2.png" class="avatar"> acrolink:</div>
<blockquote>
<p>SELECT DISTINCT ON (b.id) b.id as book_id, r.id as record_id, r.due_for_return, r.returned_at FROM books b left join records r on b.id = r.book_id order by b.id, r.id DESC</p>
</blockquote>
</aside>
<p>Hi acrolink et al,<br>
I know I’m a bit late to this party but I was reading this for inspiration on a similar issue today and I ended up just using a fragment like so:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">books_query =
  from(
    b in Mango.Books.Book,
    left_join: r in Mango.Records.Record,
    on: r.book_id == b.id,
    select: %{
      :book_id =&gt; fragment("distinct on (?) ?", b.id, b.id),
      :record_id =&gt; r.id,
      :due_for_return =&gt; r.due_for_return,
      :returned_at =&gt; r.returned_at
    },
    order_by: [asc: b.id, desc: r.due_for_return]
  )
</code></pre>
<p>My particular case didn’t allow for the methods outlined above so I gave that a go and it worked.</p>
<p>Also, I’ve sorted by due_for_return instead of id just in case something crazy happened and your record.id’s ended up out of sync with due_for_return.</p>
<p>I’d be interested to see how this pans out for you.</p>
<p>S.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="136049" data-batch-url="/posts/batch_likers">
                        7
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-use-postgresqls-distinct-on-in-ecto-query/14337/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-136049" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="136049"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>