Home » SQL & PL/SQL » SQL & PL/SQL » Case Statement (Oracle 11g )
Case Statement [message #603175] Thu, 12 December 2013 01:01 Go to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 #603193 is a reply to message #603191] Thu, 12 December 2013 02:26 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
srinivas.k2005 wrote on Thu, 12 December 2013 09:22
Sorry ! I missed... There are three address type , P, Q, R

So explain the rule clearly in terms of the returned output.
Re: Case Statement [message #603195 is a reply to message #603193] Thu, 12 December 2013 02:42 Go to previous messageGo to next message
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 #603197 is a reply to message #603195] Thu, 12 December 2013 02:43 Go to previous messageGo to next message
srinivas.k2005
Messages: 404
Registered: August 2006
Senior Member
Result Expected:
ID Custname AddressType
---- -------- ----------
1 John P
2 Kumar P
3 Martin Q
4 Dennis P
5 Raj Q
Re: Case Statement [message #603199 is a reply to message #603197] Thu, 12 December 2013 02:45 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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:28
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


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 Go to previous messageGo to next message
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:15
I 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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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
Re: Case Statement [message #603210 is a reply to message #603209] Thu, 12 December 2013 03:11 Go to previous message
srinivas.k2005
Messages: 404
Registered: August 2006
Senior Member
Sorry John... I wanted to make sure not using group by clause.

Regards,
SRK
Previous Topic: Fetch data with no rows in a table
Next Topic: Query to find tables/views with cross referencing foreign keys
Goto Forum:
  


Current Time: Fri Apr 19 08:42:21 CDT 2024