Solved

Access Join Query

Posted on 2016-10-05
12
52 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 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

808 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