David L. Hansen
asked on
Help with a SQL Query
My tables look like this:
TableA
-------------
Emp_id |Name
11 | John
18 | Karen
21 | Tim
TableB
--------------
Visit_id | City
1 | Moscow
2 | Tokyo
3 | Frankfurt
4 | Miami
TableC
-------------
Emp_id | Visit_id
11 | 2
21 | 1
11 | 2
18 | 4
18 | 3
18 | 2
I need a query that will take the tables above and do this:
Emp_id | Name | Moscow | Tokyo | Frankfurt | Miami
11 | John | | X | |
18 | Karen | | X | X | X
21 | Tim | X | | |
Total: | | 1 | 2 | 1 | 1
Note: I don't mind having to hardcode the cities into the query (in fact, that might be a plus because I'll want control over the order of the city columns). Also, the 'X's don't have to be X's...they can be replaced by 1's (or whatever). Finally, the totals-line in the example is not absolutely necessary (I'm exporting this to Excel and the users could add that themselves).
Appreciate it.
TableA
-------------
Emp_id |Name
11 | John
18 | Karen
21 | Tim
TableB
--------------
Visit_id | City
1 | Moscow
2 | Tokyo
3 | Frankfurt
4 | Miami
TableC
-------------
Emp_id | Visit_id
11 | 2
21 | 1
11 | 2
18 | 4
18 | 3
18 | 2
I need a query that will take the tables above and do this:
Emp_id | Name | Moscow | Tokyo | Frankfurt | Miami
11 | John | | X | |
18 | Karen | | X | X | X
21 | Tim | X | | |
Total: | | 1 | 2 | 1 | 1
Note: I don't mind having to hardcode the cities into the query (in fact, that might be a plus because I'll want control over the order of the city columns). Also, the 'X's don't have to be X's...they can be replaced by 1's (or whatever). Finally, the totals-line in the example is not absolutely necessary (I'm exporting this to Excel and the users could add that themselves).
Appreciate it.
ASKER
That's amazing! Thanks so much. The points are yours.
Could you give some pointers on how I can manipulate the order of the columns?
Could you give some pointers on how I can manipulate the order of the columns?
check if this helps.
Transform count(TableB.City) as cnt
select TableA.Emp_id,TableA.Name
from ((TableA
inner join TableC on TableA.Emp_id = TableC.Emp_id)
inner join TableB on TableB.Visit_id = TableC.Visit_id)
where TableB.City IN ('Moscow','Tokyo','Frankfurt','Miami')
group by TableA.Emp_id,TableA.Name
pivot TableB.City;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very nice!
ASKER
If you are still here Sharath I'm about to post another question about exporting this query into an excel spreadsheet. I've got this working except that all the columns are the same width (a bit too narrow).
I'm using this code:
Here is the link:
https://www.experts-exchange.com/questions/28124952/Help-with-formatting-column-widths-in-vba-upon-export-to-excel-from-access.html
I'm using this code:
DoCmd.OutputTo acOutputQuery, "OptCouncilRpt", acFormatXLS, "C:\Temp\OptCouncilRpt" & Format(Date, "yyyymmdd") & ".xls"
Here is the link:
https://www.experts-exchange.com/questions/28124952/Help-with-formatting-column-widths-in-vba-upon-export-to-excel-from-access.html
Open in new window