Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>SQL Server supports <code>Common Table Expression</code> and <code>Windowing Function</code>. The query uses <code>DENSE_RANK()</code> which gives a rank value according to greatest <code>Paid</code> for every <code>Account</code>. So the first value is the greatest value and has rank value of <code>1</code>. This is the value that you are going to filter.</p> <p>Assuming the column that links the two tables is <code>Client</code></p> <pre><code>WITH record AS ( SELECT a.Client, a.Account, b.Paid, b.Date, DENSE_RANK() OVER (PARTITION BY a.Client ORDER BY b.Paid DESC) rn FROM Customer a INNER JOIN Data b ON a.Client = b.Client ) SELECT Client, Account, Paid, Date FROM record WHERE rn = 1 </code></pre> <ul> <li><a href="http://www.sqlfiddle.com/#!3/98475/1" rel="nofollow">SQLFiddle Demo</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/ms189798.aspx" rel="nofollow">TSQL Ranking Functions</a></li> </ul> <p>Consider the following records (from <a href="http://www.sqlfiddle.com/#!3/98475/1" rel="nofollow">SQLFiddle Demo</a>)</p> <p><strong>Customer table</strong></p> <pre><code>╔════════╦═════════╗ ║ CLIENT ║ ACCOUNT ║ ╠════════╬═════════╣ ║ 1 ║ Stack ║ ║ 2 ║ Over ║ ║ 3 ║ Flow ║ ╚════════╩═════════╝ </code></pre> <p><strong>Data Table</strong></p> <pre><code>╔════════╦══════╦════════════════════════════════╗ ║ CLIENT ║ PAID ║ DATE ║ ╠════════╬══════╬════════════════════════════════╣ ║ 1 ║ 123 ║ January, 01 2013 00:00:00+0000 ║ ║ 1 ║ 234 ║ January, 02 2013 00:00:00+0000 ║ ║ 1 ║ 33 ║ January, 03 2013 00:00:00+0000 ║ ║ 2 ║ 674 ║ January, 04 2013 00:00:00+0000 ║ ║ 2 ║ 2424 ║ January, 05 2013 00:00:00+0000 ║ ║ 2 ║ 4545 ║ January, 06 2013 00:00:00+0000 ║ ║ 3 ║ 22 ║ January, 07 2013 00:00:00+0000 ║ ║ 3 ║ 3456 ║ January, 08 2013 00:00:00+0000 ║ ║ 3 ║ 445 ║ January, 09 2013 00:00:00+0000 ║ ╚════════╩══════╩════════════════════════════════╝ </code></pre> <hr> <p><strong>The result of Joined Table</strong></p> <pre><code>╔════════╦═════════╦══════╦════════════════════════════════╗ ║ CLIENT ║ ACCOUNT ║ PAID ║ DATE ║ ╠════════╬═════════╬══════╬════════════════════════════════╣ ║ 1 ║ Stack ║ 123 ║ January, 01 2013 00:00:00+0000 ║ ║ 1 ║ Stack ║ 234 ║ January, 02 2013 00:00:00+0000 ║ &lt;&lt; Expected Output ║ 1 ║ Stack ║ 33 ║ January, 03 2013 00:00:00+0000 ║ ║ 2 ║ Over ║ 674 ║ January, 04 2013 00:00:00+0000 ║ ║ 2 ║ Over ║ 2424 ║ January, 05 2013 00:00:00+0000 ║ ║ 2 ║ Over ║ 4545 ║ January, 06 2013 00:00:00+0000 ║ &lt;&lt; Expected Output ║ 3 ║ Flow ║ 22 ║ January, 07 2013 00:00:00+0000 ║ ║ 3 ║ Flow ║ 3456 ║ January, 08 2013 00:00:00+0000 ║ &lt;&lt; Expected Output ║ 3 ║ Flow ║ 445 ║ January, 09 2013 00:00:00+0000 ║ ╚════════╩═════════╩══════╩════════════════════════════════╝ </code></pre> <p><strong>THE FINAL OUTPUT</strong></p> <pre><code>╔════════╦═════════╦══════╦════════════════════════════════╗ ║ CLIENT ║ ACCOUNT ║ PAID ║ DATE ║ ╠════════╬═════════╬══════╬════════════════════════════════╣ ║ 1 ║ Stack ║ 234 ║ January, 02 2013 00:00:00+0000 ║ ║ 2 ║ Over ║ 4545 ║ January, 06 2013 00:00:00+0000 ║ ║ 3 ║ Flow ║ 3456 ║ January, 08 2013 00:00:00+0000 ║ ╚════════╩═════════╩══════╩════════════════════════════════╝ </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. 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