Solved

# Access Join Query

Posted on 2016-10-05
47 Views
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.

Joel
0
Question by:Genius123
• 5
• 4
• 3

LVL 16

Expert Comment

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
``````

Enjoy!
0

LVL 3

Expert Comment

@Pawan,
Looks like asker needs it in Access.

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

LVL 16

Expert Comment

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
``````
0

Author Comment

Thanks for your response.  However, I get this error:
0

LVL 16

Expert Comment

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

Now I get this:

0

LVL 3

Expert Comment

put word as before value1

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

same to the other values
0

Author Comment

I'm sorry, I don't understand.  Your example is the same as the previous one.
0

LVL 3

Accepted Solution

bfuchs earned 500 total points
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
``````
0

Author Comment

Thank you.  Worked perfectly.
0

LVL 16

Expert Comment

Ohhh ... I missed the as keyword in my query.

Great ! thnx bfuchs
0

Author Closing Comment

Thank you.  Worked perfectly.
0

## Featured Post

### Suggested Solutions

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 …