Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a solution that does a <code>GROUP BY</code> in the derived table subquery so you get only one row per movie title, and calculate the <code>ROW_NUMBER()</code> from that. </p> <p>Then join the result of the derived table to <code>st</code> again in the outer query. There will still be multiple rows per movie title, but the <code>RowNum</code> will repeat so you can filter for your <code>@pageIndex</code> correctly.</p> <pre><code>SELECT * FROM ( SELECT ROW_NUMBER() OVER(ORDER BY " + orderField + @") AS RowNum, mt.ID AS mt_ID, mt.title AS mt_title, [...] FROM mt AS mt INNER JOIN sttable AS st ON mt.ID =st.ID WHERE mt.title = @variable GROUP BY mt.ID ) mt1 INNER JOIN sttable AS st1 ON (mt1.ID = st1.ID) WHERE mt1.RowNum BETWEEN ((@pageIndex - 1) * @pageSize + 1) AND @pageIndex*@pageSize; </code></pre> <p>You'll have to loop over the result of the outer query in your display code and begin a new row of output whenever the value <code>RowNum</code> changes. This is a pretty obvious technique.</p> <p>If you instead want to do something like MySQL's <code>GROUP_CONCAT()</code> function, this is tricky in Microsoft SQL Server (I assume you're using Microsoft). </p> <p>See blogs like <a href="http://blog.shlomoid.com/2008/11/emulating-mysqls-groupconcat-function.html" rel="nofollow noreferrer">http://blog.shlomoid.com/2008/11/emulating-mysqls-groupconcat-function.html</a> that describe use of the <code>FOR xml PATH ('')</code> trick.</p> <p>PS: Your sample SQL query didn't make sense given your verbal description, so I did my best to write something sensible. No guarantees it matches your schema. </p> <hr> <p>Re your comment: I don't think you need to order by any column of <code>st</code> inside the subquery. I intended there to be no columns from <code>st</code> included in the select-list of the subquery. The only reason there's an instance of <code>st</code> joined in the subquery is to restrict the rows of <code>mt</code> to those that have matching rows in <code>st</code>. But the <code>GROUP BY mt.ID</code> makes sure there's only one row per row of <code>mt</code> (actually since this is SQL Server and not MySQL you'll need to name all the <code>mt</code> columns of the select-list in the <code>GROUP BY</code> clause).</p> <hr> <p>Re your second comment:</p> <blockquote> <p>I want to first display mt rows that have corresponding st records that have been most recently added</p> </blockquote> <p>You can add other columns to the grouped query if you use grouping functions. For instance, the latest <code>date_added</code> per group of <code>mt</code> is <code>MAX(st.date_added)</code>, and you can add this column to the subquery.</p> <p>However, don't use <code>ORDER BY</code> in the subquery. There's seldom any reason to sort a subquery, since the order may be altered anyway by using the subquery result in the JOIN or other operations you'd use a subquery in. </p> <p>You should sort in the outer query:</p> <pre><code>SELECT * FROM ( SELECT ROW_NUMBER() OVER(ORDER BY " + orderField + @") AS RowNum, mt.ID AS mt_ID, mt.title AS mt_title, [...] -- other mt.* columns MAX(st.date_added) AS latest_date_added FROM mt AS mt INNER JOIN sttable AS st ON mt.ID =st.ID WHERE mt.title = @variable GROUP BY mt.ID, -- other mt.* columns ) mt1 INNER JOIN sttable AS st1 ON (mt1.ID = st1.ID) WHERE mt1.RowNum BETWEEN ((@pageIndex - 1) * @pageSize + 1) AND @pageIndex*@pageSize ORDER BY mt1.latest_date_added DESC, st1.date_added DESC; </code></pre>
    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. 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