Solved

Access Join Query

Posted on 2016-10-05
12
53 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 28

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 28

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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

Author Comment

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

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 28

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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…
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 …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

756 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