-
Join Union ...grouping records
Hi,
I basically have three tables from which i need to fetch the data which i am doing using inner joins and its working fine...
But now the problem is that i need to group the resultant data using column in one of the table satisfying certain conditions i.e
I have something like this :
SELECT first_table.field1, first_table.field2, second_table.field3, second_table.field4, third_table.field5, third_table.field6
FROM first_table
INNER JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield
INNER JOIN third_table
ON second_table.keyfield = third_table.foreign_keyfield
But now i want to further group the resultant records into different groups depending upon something like :
where third_table.field5 in (' val1', ' val2')
where third_table.field5 in (' val3', ' val4')
where third_table.field5 in (' val5', ' val6')
Can anyone Please help me with this second part...
-
Please take the following as untested pseudo-code under the assumption you are trying to do a group by
Code:
select ...,
decode(third_table.field5,val1,1,val2,1,val3,2,val4,2,val5,3,val6,3) "MYGROUP"
from
(
SELECT first_table.field1, first_table.field2, second_table.field3, second_table.field4, third_table.field5, third_table.field6
FROM first_table
INNER JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield
INNER JOIN third_table
ON second_table.keyfield = third_table.foreign_keyfield
)
group by "MYGROUP"
;
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|