Solved

Access Join Query

Posted on 2016-10-05
12
55 Views
Last Modified: 2016-10-25
Hello,

I'm trying to join two records into one and am having trouble.  I made a simple example of what I'm trying to do in this PDF: table.pdf

The Excel file is just so I could show you the results I want.

Thanks for your help!
Joel
0
Comment
Question by:Genius123
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41830909
Hi,

Try below.

SELECT a.Job, a.Type
, MAX(CASE WHEN a.Name = 'B' THEN a.Amount END) Value1 
, MAX(CASE WHEN a.Name = 'A' THEN a.Amount END) - MAX(CASE WHEN a.Name = 'B' THEN a.Amount END) Value2
, MAX(CASE WHEN a.Name = 'A' THEN a.Amount END) - MAX(CASE WHEN a.Name = 'B' THEN a.Amount END) - MAX(CASE WHEN a.Name = 'B' THEN a.Amount END) Value3
FROM 
tblTest a 
GROUP BY a.Type, a.Job

Open in new window


Enjoy!
0
 
LVL 4

Expert Comment

by:bfuchs
ID: 41830924
@Pawan,
Looks like asker needs it in Access.

So instead of case when, use equivalent function,
iif(a.name = 'B', a.amount,0)

Open in new window

1
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41830929
Thank you bfuchs. Updated my query.

SELECT a.Job, a.Type
, MAX(iif(a.name = 'B', a.amount,0)) Value1 
, MAX(iif(a.name = 'B', a.amount,0)) - MAX(iif(a.name = 'B', a.amount,0)) Value2
, MAX(iif(a.name = 'A', a.amount,0)) - MAX(iif(a.name = 'B', a.amount,0)) - MAX(iif(a.name = 'B', a.amount,0)) Value3
FROM 
tblTest a 
GROUP BY a.Type, a.Job

Open in new window

0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:Genius123
ID: 41831995
Thanks for your response.  However, I get this error: error
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41832257
Try below..

SELECT a.Job, a.Type
, MAX(iif(a.name = 'B', a.amount,0)) Value1
, MAX(iif(a.name = 'B', a.amount,0)) - MAX(iif(a.name = 'B', a.amount,0)) Value2
, MAX(iif(a.name = 'A', a.amount,0)) - MAX(iif(a.name = 'B', a.amount,0)) - MAX(iif(a.name = 'B', a.amount,0)) Value3
FROM
tblTest a
GROUP BY a.Type, a.Job
0
 

Author Comment

by:Genius123
ID: 41832276
Now I get this:

error
0
 
LVL 4

Expert Comment

by:bfuchs
ID: 41832565
put word as before value1

like
MAX(iif(a.name = 'B', a.amount,0))  as Value1 

Open in new window


same to the other values
0
 

Author Comment

by:Genius123
ID: 41837341
I'm sorry, I don't understand.  Your example is the same as the previous one.
0
 
LVL 4

Accepted Solution

by:
bfuchs earned 500 total points
ID: 41837385
I added word as before value1.

try this

SELECT a.Job, a.Type
, MAX(iif(a.name = 'B', a.amount,0)) as Value1 
, MAX(iif(a.name = 'B', a.amount,0)) - MAX(iif(a.name = 'B', a.amount,0)) as Value2
, MAX(iif(a.name = 'A', a.amount,0)) - MAX(iif(a.name = 'B', a.amount,0)) - MAX(iif(a.name = 'B', a.amount,0)) as Value3
FROM 
tblTest a 
GROUP BY a.Type, a.Job

Open in new window

0
 

Author Comment

by:Genius123
ID: 41837477
Thank you.  Worked perfectly.
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41837488
Ohhh ... I missed the as keyword in my query.

Great ! thnx bfuchs
0
 

Author Closing Comment

by:Genius123
ID: 41859372
Thank you.  Worked perfectly.
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

726 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