Home » SQL & PL/SQL » SQL & PL/SQL » Complicate Query (Oracle SQL )
Complicate Query [message #630256] |
Tue, 23 December 2014 17:27 |
raajes_n
Messages: 33 Registered: April 2005 Location: India
|
Member |
|
|
Hi All,
Am having one typical requirement as follows
CustId TELN_Type Primary_Code SubscriberNo
100 Home 44 26425218
100 Mobile 1234567890
100 Emergency 44 56565656
101 Home
101 Mobile 92929292922
102 Emergency 44 32323232
Am having the above table structure with data
My task is I need to check
1. If user is having home, Mobile, Emergency (all 3 not null)then I need to pick Primary_Code and SubscriberNo for Home and Mobile alone
2. If user is not having home telephone then I need to check for Mobile then I need to pick respective Primary_Code and SubscriberNo
3. If user is not having both Home and Mobile, then I need to pick Emergency
My output should be
CustId TelnType1 PrimaryCode1 SubscriberNo1 TelnType2 PrimaryCode2 SubscriberNo2
100 Home 44 26425218 Mobile 1234567890
101 Mobile 92929292922
102 Emergency 44 32323232
I've included the above requirement in Excel format also.
Need your help. Thanks in Advance
[Updated on: Wed, 24 December 2014 06:54] by Moderator Report message to a moderator
|
|
|
|
|
|
Re: Complicate Query [message #630298 is a reply to message #630295] |
Wed, 24 December 2014 04:41 |
raajes_n
Messages: 33 Registered: April 2005 Location: India
|
Member |
|
|
insert into custinfo values(1,100,'Home','044','22222222');
insert into custinfo values(2,100,'Mobile','','1234567890');
insert into custinfo values(3,100,'Emergency','044','2222222222');
insert into custinfo values(4,101,'Home','','');
insert into custinfo values(5,101,'Mobile','','0987654321');
insert into custinfo values(6,102,'Emergency','092','4444444444');
|
|
|
Re: Complicate Query [message #630299 is a reply to message #630298] |
Wed, 24 December 2014 04:43 |
raajes_n
Messages: 33 Registered: April 2005 Location: India
|
Member |
|
|
Sorry Michael
create table CUSTINFO
(
sid NUMBER not null,
custid NUMBER,
telntype VARCHAR2(10),
primarycode VARCHAR2(5),
subscriberno VARCHAR2(25)
)
insert into custinfo values(1,100,'Home','044','22222222');
insert into custinfo values(2,100,'Mobile','','1234567890');
insert into custinfo values(3,100,'Emergency','044','2222222222');
insert into custinfo values(4,101,'Home','','');
insert into custinfo values(5,101,'Mobile','','0987654321');
insert into custinfo values(6,102,'Emergency','092','4444444444');
|
|
|
|
|
|
|
Re: Complicate Query [message #630314 is a reply to message #630310] |
Wed, 24 December 2014 06:51 |
|
Michel Cadot
Messages: 68647 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
SQL> with
2 data as (
3 select c.*,
4 row_number() over
5 (partition by c.custid
6 order by decode(telntype, 'Emergency',1, 'Mobile',2, 'Home',3, 4)) rk
7 from CUSTINFO c
8 )
9 select custid,
10 max(decode(rk, 1, telntype)) telntype1,
11 max(decode(rk, 1, primarycode)) primarycode1,
12 max(decode(rk, 1, subscriberno)) subscriberno1,
13 max(decode(rk, 2, telntype)) telntype2,
14 max(decode(rk, 2, primarycode)) primarycode2,
15 max(decode(rk, 2, subscriberno)) subscriberno2,
16 max(decode(rk, 3, telntype)) telntype3,
17 max(decode(rk, 3, primarycode)) primarycode3,
18 max(decode(rk, 3, subscriberno)) subscriberno3
19 from data
20 group by custid
21 order by custid
22 /
CUSTID TELNTYPE1 PRIMA SUBSCRIBERNO1 TELNTYPE2 PRIMA SUBSCRIBERNO2 TELNTYPE3 PRIMA SUBSCRIBERNO3
---------- ---------- ----- ------------- ---------- ----- ------------- ---------- ----- -------------
100 Emergency 044 2222222222 Mobile 1234567890 Home 044 22222222
101 Mobile 0987654321 Home
102 Emergency 092 4444444444
3 rows selected.
[Updated on: Wed, 24 December 2014 06:53] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Apr 26 15:00:45 CDT 2024
|