Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This question was quite interesting. The problem is solved using an aggregate query.</p> <p>SQL Fiddle: <a href="http://www.sqlfiddle.com/#!3/29256/1/0" rel="nofollow">http://www.sqlfiddle.com/#!3/29256/1/0</a></p> <pre><code>select d.[Client], c.[Account], max([Paid])as 'PAID', d.[Date] from Customer c, Data d where d.[Client] = c.[Client] group by d.[Client],c.[Account],d.[Date]; </code></pre> <p>Produces the Results:</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 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 3 Flow 22 January, 07 2013 00:00:00+0000 3 Flow 3456 January, 08 2013 00:00:00+0000 3 Flow 445 January, 09 2013 00:00:00+0000 </code></pre> <p>If we had the situation where a Client Could have Many Accounts and we wanted to find the Client-Account with the largest Amount Paid for a specific date we would have to do something more complicated:</p> <pre><code>select * from Customer c2, Data d2, ( select d.[Client], max([Paid])as 'PAID', d.[Date] from Customer c, Data d where d.[Client] = c.[Client] group by d.[Client],d.[Date] ) subQ where c2.[client] = d2.[client] and c2.[client] = subQ.[client] and d2.[Paid] = subQ.[Paid] and d2.[Date] = subQ.[Date]; </code></pre> <p>As you cannot add additional columns to a query that are not part of the group by - see: <a href="http://technicalsmile.blogspot.gr/2011/04/column-is-invalid-in-select-list.html" rel="nofollow">http://technicalsmile.blogspot.gr/2011/04/column-is-invalid-in-select-list.html</a></p>
    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.
    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