Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a View

Status
Not open for further replies.

sim133

Technical User
Sep 14, 2004
85
US
Hi all,
I don’t have a deep experience in SQL so I am not really sure whether this is doable or not. I have this table Event Table that keeps track of event date. Each event date has Event code. Here is an Examle
Code:
Event Data    Event Code

Event Data    Event Code

12/12/2000     RR
12/12/2002     RR 
12/12/2000     BB
12/12/2002     BB
12/12/2000     CC
12/12/2002     CC



I want to change the Event Code into a Column field and  have the values under that.. 
Example:


RR          BB            CC
12/12/2000  12/12/2000   12/12/2000
12/12/2002  12/12/2002   12/12/2002
As I said I am not sure whether this is doable, but I appreciate for any help.
 

Search for crosstab or pivot table.


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
I would do this using a Decode and group function

Select Group_field,
Max(decode( Event_code, 'RR', Event_date, to_date('01/01/1900', 'DD/MM/YYY') RR,
Max(decode( Event_code, 'BB', Event_date, to_date('01/01/1900', 'DD/MM/YYY') BB,
Max(decode( Event_code, 'CC', Event_date, to_date('01/01/1900', 'DD/MM/YYY') RR
FROM TableName
Group by GroupField

Decode is like If then else

If Event_code = 'RR' then Event_date else default_date

The query returns 3 rows but the group function reduces to a single row, showing only max data.

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top