Solved

Access Join Query

Posted on 2016-10-05
12
47 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
  • 5
  • 4
  • 3
12 Comments
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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 3

Expert Comment

by:bfuchs
Comment Utility
@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 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
 

Author Comment

by:Genius123
Comment Utility
Thanks for your response.  However, I get this error: error
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
Comment Utility
Now I get this:

error
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 3

Expert Comment

by:bfuchs
Comment Utility
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
Comment Utility
I'm sorry, I don't understand.  Your example is the same as the previous one.
0
 
LVL 3

Accepted Solution

by:
bfuchs earned 500 total points
Comment Utility
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
Comment Utility
Thank you.  Worked perfectly.
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Ohhh ... I missed the as keyword in my query.

Great ! thnx bfuchs
0
 

Author Closing Comment

by:Genius123
Comment Utility
Thank you.  Worked perfectly.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now