Case Statement [message #603175] |
Thu, 12 December 2013 01:01 |
srinivas.k2005
Messages: 404 Registered: August 2006
|
Senior Member |
|
|
There are two address code for each Customer, (P, Q) in the column AddressType.
I want to retrieve data for each Customer having the AddressType "P".
If AddressType "P" does not exists for that customer, I need to retrieve date for "Q"
Create table T(
ID number,
Custname Varchar2(20),
AddressType CHAR(1)
)
Insert into T values (1, 'John', 'P');
Insert into T values (1, 'John', 'Q');
Insert into T values (2, 'Kumar', 'P');
Insert into T values (2, 'kumar', 'Q');
Insert into T values (3, 'Martin', 'Q');
Insert into T values (4, 'Dennis', 'P');
Insert into T values (4, 'Dennis', 'Q');
Insert into T values (5, 'Raj', 'Q');
Result Expected:
ID Custname AddressType
---- -------- ----------
1 John P
2 Kumar P
3 Martin Q
4 Dennis P
5 Raj Q
Please help!!
Regards,
SRk
|
|
|
Re: Case Statement [message #603180 is a reply to message #603175] |
Thu, 12 December 2013 01:41 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
SQL> SELECT ID, CUSTNAME, ADDRESSTYPE
2 FROM (SELECT T.*,
3 ROW_NUMBER() OVER(PARTITION BY LOWER(CUSTNAME) ORDER BY ID) RN
4 FROM T)
5 WHERE RN = 1
6 ORDER BY ID;
ID CUSTNAME ADDRESSTYPE
---------- -------------------- -----------
1 John P
2 Kumar P
3 Martin Q
4 Dennis P
5 Raj Q
|
|
|
Re: Case Statement [message #603185 is a reply to message #603175] |
Thu, 12 December 2013 01:58 |
srinivas.k2005
Messages: 404 Registered: August 2006
|
Senior Member |
|
|
Here I gave an sample example. But Custname can be alphanumberic... In that case below query fails.
Examples for Custname:
1. 0184 BRUMMERIA EXT 14
2. 2021 Bryanston
3. 2111 dd
Regards,
SRK
|
|
|
Re: Case Statement [message #603187 is a reply to message #603185] |
Thu, 12 December 2013 02:06 |
|
dariyoosh
Messages: 538 Registered: March 2009 Location: France
|
Senior Member |
|
|
The second data sample you posted has nothing to do with the first one, where is the address type? I agree by the row_number that Lilit suggested except that I will order by address type not by the id.
select t2.*
from
(
select t1.*,
row_number()
over (partition by t1.custname order by t1.addresstype asc) rn
from t t1
) t2
where t2.rn = 1;
ID CUSTNAME A RN
---------- -------------------- - ----------
4 Dennis P 1
1 John P 1
2 Kumar P 1
3 Martin Q 1
5 Raj Q 1
2 kumar Q 1
6 rows selected.
[Updated on: Thu, 12 December 2013 02:09] Report message to a moderator
|
|
|
Re: Case Statement [message #603190 is a reply to message #603175] |
Thu, 12 December 2013 02:14 |
John Watson
Messages: 8929 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
You don't need to use an analytic query:orclz> select id,custname,min(addresstype) from t group by id,custname order by id;
ID CUSTNAME M
---------- -------------------- -
1 John P
2 Kumar P
2 kumar Q
3 Martin Q
4 Dennis P
5 Raj Q
6 rows selected.
orclz> A simpler execution plan.
|
|
|
Re: Case Statement [message #603191 is a reply to message #603190] |
Thu, 12 December 2013 02:22 |
srinivas.k2005
Messages: 404 Registered: August 2006
|
Senior Member |
|
|
Sorry ! I missed... There are three address type , P, Q, R
In the above query, There are two customernames with P and Q.
ID CUSTNAME M
---------- -------------------- -
1 John P
2 Kumar P
2 kumar Q
3 Martin Q
4 Dennis P
5 Raj Q
Regards,
SRK
|
|
|
|
Re: Case Statement [message #603195 is a reply to message #603193] |
Thu, 12 December 2013 02:42 |
srinivas.k2005
Messages: 404 Registered: August 2006
|
Senior Member |
|
|
Rule Updated:
There are three address code for each Customer, (P, Q, R) in the column AddressType.
I want to retrieve data for each Customer having the AddressType "P".
If AddressType "P" does not exists for that customer, I need to retrieve date for "Q" ( "R" should not be retrieved at all).
Create table T(
ID number,
Custname Varchar2(20),
AddressType CHAR(1)
)
select * from T
Insert into T values (1, 'John', 'P');
Insert into T values (1, 'John', 'Q');
Insert into T values (1, 'John', 'R');
Insert into T values (2, 'kumar', 'P');
Insert into T values (2, 'kumar', 'Q');
Insert into T values (2, 'kumar', 'R');
Insert into T values (3, 'Martin', 'R');
Insert into T values (3, 'Martin', 'Q');
Insert into T values (4, 'Dennis', 'P');
Insert into T values (4, 'Dennis', 'Q');
Insert into T values (5, 'Raj', 'R');
Insert into T values (5, 'Raj', 'Q');
Regards,
SRK
[EDITED by LF: removed superfluous empty lines]
[Updated on: Thu, 19 December 2013 13:40] by Moderator Report message to a moderator
|
|
|
|
Re: Case Statement [message #603199 is a reply to message #603197] |
Thu, 12 December 2013 02:45 |
srinivas.k2005
Messages: 404 Registered: August 2006
|
Senior Member |
|
|
I did small change for query given by Lalit...
SELECT ID, CUSTNAME, ADDRESSTYPE
FROM (SELECT T.*,
ROW_NUMBER() OVER(PARTITION BY ID ORDER BY AddressType) RN
FROM T)
WHERE RN = 1
ORDER BY ID;
Thanks a lot to all...
|
|
|
Re: Case Statement [message #603202 is a reply to message #603185] |
Thu, 12 December 2013 02:48 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
srinivas.k2005 wrote on Thu, 12 December 2013 13:28Here I gave an sample example. But Custname can be alphanumberic... In that case below query fails.
Examples for Custname:
1. 0184 BRUMMERIA EXT 14
2. 2021 Bryanston
3. 2111 dd
You mean to say "0184 BRUMMERIA EXT 14" is a customer NAME?
|
|
|
Re: Case Statement [message #603204 is a reply to message #603199] |
Thu, 12 December 2013 02:56 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
srinivas.k2005 wrote on Thu, 12 December 2013 14:15I did small change for query given by Lalit...
First of all, "Kumar" and "kumar" are not same if case sensitivity is considered. So, you won't get your desired output. For each addresstype, the query will return a row for "Kumar" and "kumar" when you need only one row.
|
|
|
Re: Case Statement [message #603207 is a reply to message #603195] |
Thu, 12 December 2013 03:03 |
John Watson
Messages: 8929 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
It is time you did a bit of thinking for yourself, rather than asking other people to do it for you, and then rejecting their solutions because your description of the problem was wrong.
You do not want any rows with R? So exclude them with a predicate.
--
update: though of course my solution will still work perfectly as written with your new test data.
[Updated on: Thu, 12 December 2013 03:07] Report message to a moderator
|
|
|
Re: Case Statement [message #603209 is a reply to message #603202] |
Thu, 12 December 2013 03:08 |
srinivas.k2005
Messages: 404 Registered: August 2006
|
Senior Member |
|
|
You mean to say "0184 BRUMMERIA EXT 14" is a customer NAME?
Data in DB(Somebody inserted) was like that...( NOT sure if it is correct).
John,
I take that
Thanks,
SRK
|
|
|
|