Solved

SQL Statement in Oracle & Access

Posted on 2012-03-12
5
372 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 77

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 77

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 77

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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 …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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