Solved

SQL Statement in Oracle & Access

Posted on 2012-03-12
5
367 Views
Last Modified: 2012-03-27
If I have these tables

Table_A
======
Emp_Id
Emp_Name
Emp_Grade_Id

Open in new window


For example
Emp_Id        Emp_Name         Emp_Grade_Id
333              John Smith          0007
666              Mikel Josef          008

Open in new window


Table_B
======
Emp_Grade_Id
Emp_Grade_Name

Open in new window


For example
Emp_Grade_Id          Emp_Grade_Name
0007                         Manager
008                           System Analysis

Open in new window


Table_C
======
Emp_Id
ALW_DED_Id
Amount

Open in new window


For example
Emp_Id                     ALW_DED_Id            Amount
333                           1000                        500
333                           1001                        50
333                           1002                        30
333                           1003                        15

Open in new window


Table_D
======
ALW_DED_Id
ALW_DED_NAME

Open in new window


For example
ALW_DED_Id            ALW_DED_NAME
1000                        Basic
1001                        Accomadation
1002                        Electrisity
1003                        Water

Open in new window


What is the SQL Statement to display the following: -
Emp_Id     Emp_Name      Emp_Grade_Name   Basic    Accomadation    Elec   Water

Open in new window


In oracle & Access
0
Comment
Question by:egovernment
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37711253
Is there a maximum number of ALW_DED_Id entries for employees in table_C?
0
 

Author Comment

by:egovernment
ID: 37711400
Is there a maximum number of ALW_DED_Id entries for employees in table_C?
4
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37711470
What version of Access are you using?

Try this for Oracle:

select a.emp_id, a.emp_name,
	b.emp_grade_name,
	max(case when c.alw_ded_id = '1000' then alw_ded_name end) basic,
	max(case when c.alw_ded_id = '1001' then alw_ded_name end) accomadation,
	max(case when c.alw_ded_id = '1002' then alw_ded_name end) electricity,
	max(case when c.alw_ded_id = '1001' then alw_ded_name end) water
from table_a a,
	table_b b,
	table_c c,
	table_d d
where a.emp_grade_id=b.emp_grade_id and 
	a.emp_id=c.emp_id and
	c.alw_ded_id=d.alw_ded_id
group by a.emp_id, a.emp_name, b.emp_grade_name
/

Open in new window

0
 

Author Comment

by:egovernment
ID: 37712812
What version of Access are you using?
Access 2003
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 37714372
First there is a typo in the SQL I posted above.

Change:        max(case when c.alw_ded_id = '1001' then alw_ded_name end) water
To:       max(case when c.alw_ded_id = '1003' then alw_ded_name end) water


Access doesn't appear to have a CASE statement in SQL but they have a SWITCH function that bascially does the same thing.

I don't have Access 2003 around but the following was tested in Access 2007 and should run fine in 2003.

SELECT table_a.Emp_Id, table_a.Emp_Name, table_b.Emp_Grade_Name, 
   max(Switch(  table_c.ALW_DED_Id="1000",alw_ded_name )) AS Basic,
   max(Switch(  table_c.ALW_DED_Id="1001",alw_ded_name )) AS Accomadation,
   max(Switch(  table_c.ALW_DED_Id="1002",alw_ded_name )) AS Electricity,
   max(Switch(  table_c.ALW_DED_Id="1003",alw_ded_name )) AS Water
FROM table_d INNER JOIN (table_c INNER JOIN (table_a INNER JOIN table_b ON table_a.Emp_Grade_Id = table_b.Emp_Grade_Id) ON table_c.Emp_Id = table_a.Emp_Id) ON table_d.ALW_DED_Id = table_c.ALW_DED_Id
group by  table_a.Emp_Id, table_a.Emp_Name, table_b.Emp_Grade_Name;

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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.

867 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

12 Experts available now in Live!

Get 1:1 Help Now