Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with a SQL Query

Posted on 2013-05-10
6
Medium Priority
?
307 Views
Last Modified: 2013-05-10
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.
0
Comment
Question by:David L. Hansen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 39157175
can you try this?
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)
 group by TableA.Emp_id,TableA.Name
pivot TableB.City;

Open in new window

0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39157205
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?
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39157233
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;

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 39157235
sorry, this.
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)
 group by TableA.Emp_id,TableA.Name
pivot TableB.City IN ('Moscow','Tokyo','Frankfurt','Miami');

Open in new window

0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39157278
Very nice!
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39157314
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:
DoCmd.OutputTo acOutputQuery, "OptCouncilRpt", acFormatXLS, "C:\Temp\OptCouncilRpt" & Format(Date, "yyyymmdd") & ".xls"

Open in new window


Here is the link:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28124952.html
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question