Note that there are some explanatory texts on larger screens.

plurals
  1. POFlatten relational data for summary/export
    primarykey
    data
    text
    <p>Say we have two tables in an MS Access db:</p> <p>Service Users:</p> <pre><code>| ID | Name | Other details... | | 1 | Joe | Blah... | | 2 | Fred | Qwerty... | | 3 | Bob | Something else...| </code></pre> <p>Teams providing services:</p> <pre><code>| ID | TeamID | UserID | | 1 | T1 | 1 | | 2 | T2 | 1 | | 3 | T2 | 2 | | 4 | T3 | 2 | | 5 | T3 | 3 | </code></pre> <p>I need to produce a summary query that produces a single row for each user, with the first several teams (by TeamID) assigned sitting in separate columns. Like:</p> <p>Query:</p> <pre><code>| UserID | Name | Team1 | Team2 | | 1 | Joe | T1 | T2 | | 2 | Fred | T2 | T3 | | 3 | Bob | T3 | Null | </code></pre> <p>I can get the Team1 column using max() from a sub select query, but I'm having a complete mental block on how to achieve Team2, Team3, etc. (Yes, I know that if there are more teams assigned to a user than I create columns the query will lose that information: that isn't a concern).</p> <p>Edit: To clarify, the number of columns in the query will be fixed (in the actual query, there will always be 7). If there are less teams than columns the additional columns should be Null (as in example). If there are more teams than columns, only the first 7 teams will be shown in this summary.</p> <p>Edit 2 - Possible solution which doesn't work...:</p> <p>I tried...</p> <pre><code>SELECT UserTable.ID As UID, UserTable.Name, (SELECT TOP 1 TeamID FROM TeamTable WHERE UserTable.ID = TeamTable.UserID ORDER BY TeamID) As Team1 FROM UserTable </code></pre> <p>... which works fine. Unfortunately...</p> <pre><code>SELECT UserTable.ID As UID, UserTable.Name, (SELECT TOP 1 TeamID FROM TeamTable WHERE UserTable.ID = TeamTable.UserID ORDER BY TeamID) As Team1, (SELECT TOP 1 TeamID FROM TeamTable WHERE UserTable.ID = TeamTable.UserID AND TeamID &lt;&gt; Team1 ORDER BY TeamID) As Team2 FROM UserTable </code></pre> <p>... throws up a parameter box for Team1. An ideas on how to skip the first/second/etc... values from an jet query?</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.
 

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