Note that there are some explanatory texts on larger screens.

plurals
  1. POsql join tomfoolery (or How can I wind up with more rows than when I started?)
    primarykey
    data
    text
    <p>M$ Access 2010</p> <p>table [CONTACT PERSON] has key field [Contact ID] and has 1599 rows. table [Person Client Address Mailing] has key field [ClientID] and has 1465 rows.</p> <p>Not all [CONTACT PERSON].[Contact ID] values have a matching [Person Client Address Mailing].[ClientID]</p> <p>So I'm buffaloed as to how this</p> <pre><code>SELECT aa.[Contact ID], bb.[cstreet1], bb.[cstreet2], bb.[cstreet3], bb.[ccity], bb.[cstate], bb.[czip], bb.[ccountry] FROM [CONTACT PERSON] AS aa LEFT JOIN [Person Client Address Mailing] AS bb ON aa.[Contact ID] = bb.[ClientID] ORDER BY aa.[Contact ID]; </code></pre> <p>can return 1606 rows. My understanding is LEFT JOIN returns every row of the main table and all matching rows of the joined table . Therefore the number of rows returned will always be equal to the number of rows in the main table, never more. What gives? Or is this more Access-specific sql trickery?</p> <p>[new data]</p> <p>Turns out the above query is exactly right - the problem lies with duplicate rows in the joined table, which gets built with this query:</p> <pre><code>SELECT aa.[ClientID], aa.[cstreet1], aa.[cstreet2], aa.[cstreet3], aa.[ccity], aa.[cstate], aa.[czip], aa.[ccountry] FROM AAAddress_X AS aa INNER JOIN ( SELECT gg.[CLIENTID], MIN(gg.[sortOrderPersonPrimary]) AS sortOrderPersonPrimary FROM AAAddress_X AS gg WHERE gg.sortOrderPersonPrimary Is Not Null GROUP BY gg.[CLIENTID]) AS bb ON aa.[CLIENTID] = bb.[CLIENTID] AND aa.[sortOrderPersonPrimary] = bb.[sortOrderPersonPrimary] WHERE aa.[cstreet1] &lt;&gt; "" ORDER BY aa.[CLIENTID]; </code></pre> <p>Running the inner query by itself yields 1458 rows, each ClientID-sortOrderPersonPrimary pairing is unique. And the sortOrderPersonPrimary value is in fact the minimum value, excellent. 1458 + 7 = 1465 so the duplicate rows must be coming in through the joining with the outer query. Which makes sense since there's 7 rows in AAAddress_X with matching ClientID-sortOrderPersonPrimary pairs.</p> <p>So, follow-on question is, how can I adjust the above query to just return only the first occurance of each ClientID with the lowest sortOrderPersonPrimary value? Or maybe a different query, I'm not fussy.</p> <p>Maybe I'm overcomplicating this, so I'll lay out what I have and what I need:</p> <p>Example: AAAddress contains some rows like this: ClientID sortOrderPersonPrimary cstreet1 cstreet2..... 10107 3 123 pine 10108 1 434 olson 10108 1 5454 main 10108 2 434 olivia 10109 2 323 poplar</p> <p>I need: 10107 3 123 pine 10108 1 434 olson 10109 2 323 poplar</p> <p>If there's no way to rid myself of these pesky duplicates in queries my Plan B is to manually pre-create an empty table [Person Client Address Mailing] with fields ClientID and sortOrderPersonPrimary both defined as keys, do an INSERT INTO SELECT... and let the keys implicitly weed out the dupes.</p> <p>Thanks for everone's help so far! You guys rock!</p> <p>Still-learning Steve</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. 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