Home » SQL & PL/SQL » SQL & PL/SQL » SQL query to retrieve one record for each employee
SQL query to retrieve one record for each employee [message #620298] Thu, 31 July 2014 01:52 Go to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi all,

Help me on writing SQL query to retrieve one record for each employee from the following table? preferably a standard SQL.

CREATE TABLE xxc_contact 
  ( 
     empnum           NUMBER, 
     alternatecontact VARCHAR2(100), 
     relation         VARCHAR2(10), 
     phtype           VARCHAR2(10), 
     phone            NUMBER 
  ); 


insert into xxc_contact values(123456,'Rick Grimes','SP','Cell',9999999999)


insert into xxc_contact values(123456,'Rick Grimes','SP','Work',8888888888)

insert into xxc_contact values(123457,'Daryl Dixon','FR','Work',7777777777)

insert into xxc_contact values(123457,'Daryl Dixon','FR','Home',3333333333)

insert into xxc_contact values(123458,'Maggie Greene','CH','Cell',5555555555)

insert into xxc_contact values(123458,'Maggie Greene','CH','Home',6666666666)

select * from xxc_contact

expected result:
-----------------
EmpNum  AlternateContact Relation PhType	Phone
123456	Rick Grimes	   SP	    Cell	9999999999
123457	Daryl Dixon	   FR	    Work	7777777777
123458	Maggie Greene	   CH	    Home	6666666666

we are trying with below code using analytical function

SELECT empnum, alternatecontact, relation, phtype, phone
  FROM (SELECT a.*, ROW_NUMBER () OVER (PARTITION BY empnum ORDER BY empnum)
                                                                            r
          FROM xxc_contact a)
 WHERE r = 1;

Getting output as
please check the attached csv file

  • Attachment: Sheet1.csv
    (Size: 0.16KB, Downloaded 971 times)
Re: SQL query to retrieve one record for each employee [message #620299 is a reply to message #620298] Thu, 31 July 2014 01:59 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
You were quite close, modified the order by clause from empnum to phone :

SQL> column alternatecontact format A20;
SQL> 
SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2    FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY phone DESC) r
  3            FROM xxc_contact a)
  4   WHERE r = 1
/
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Cell       9999999999
    123457 Daryl Dixon          FR         Work       7777777777
    123458 Maggie Greene        CH         Home       6666666666



Regards,
Lalit
Re: SQL query to retrieve one record for each employee [message #620300 is a reply to message #620298] Thu, 31 July 2014 02:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
please check the attached csv file


Many of us can't or don't want to download files, so post them inline in text mode and post them formatted.

In addition, explain with words the result you want.

Re: SQL query to retrieve one record for each employee [message #620302 is a reply to message #620298] Thu, 31 July 2014 02:01 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Does it matter which row you end up with? Should it be Mr Grimes's cell phone or work phone?
Re: SQL query to retrieve one record for each employee [message #620303 is a reply to message #620299] Thu, 31 July 2014 02:04 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Thanks Lalit... Smile working fine now
Re: SQL query to retrieve one record for each employee [message #620304 is a reply to message #620302] Thu, 31 July 2014 02:06 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
Does it matter which row you end up with? Should it be Mr Grimes's cell phone or work phone?

Hi John , This is a Mr Grimes's cell phone
Re: SQL query to retrieve one record for each employee [message #620305 is a reply to message #620300] Thu, 31 July 2014 02:07 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi Michel,

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2    FROM (SELECT a.*, ROW_NUMBER () OVER (PARTITION BY empnum ORDER BY empnum)
  3                                                                              r
  4            FROM xxc_contact a)
  5   WHERE r = 1;

    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Cell       9999999999
    123457 Daryl Dixon          FR         Work       7777777777
    123458 Maggie Greene        CH         Cell       5555555555


Re: SQL query to retrieve one record for each employee [message #620306 is a reply to message #620303] Thu, 31 July 2014 02:13 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
mist598 wrote on Thu, 31 July 2014 12:34
Thanks Lalit... Smile working fine now


Alternatively, you can also use MAX(phone) instead of row_number().

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2    FROM (SELECT a.*, MAX(phone) over(PARTITION BY empnum) rn FROM xxc_contact a)
  3   WHERE phone = rn
  4  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Cell       9999999999
    123457 Daryl Dixon          FR         Work       7777777777
    123458 Maggie Greene        CH         Home       6666666666

[Updated on: Thu, 31 July 2014 02:15]

Report message to a moderator

Re: SQL query to retrieve one record for each employee [message #620308 is a reply to message #620306] Thu, 31 July 2014 02:17 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Thanks Lalit...

the data comes out that is order phone records right?
Re: SQL query to retrieve one record for each employee [message #620309 is a reply to message #620308] Thu, 31 July 2014 02:22 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
You did not mention the rules for your SQL. So looking at your expected result, based on the pattern of phone I assumed you need the row which has the max phone number.

Using row_number, Oracle assigns ranking based on the descending order of phone withing each group of empnum. And finally selects those rows which has least rank(least since the order is descending for phone).

Using MAX, Oracle assignes the maximum phone to all the rows in each group of empnum. And finally selects the rows having the maximum phone. Order by clause is ommitted here intentionally. You can find out why.
Re: SQL query to retrieve one record for each employee [message #620310 is a reply to message #620305] Thu, 31 July 2014 02:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
mist598 wrote on Thu, 31 July 2014 09:07
Hi Michel,
...


Too late, this must be given with question not with the solution.
Your test case is still invalid: no ";" at the end of insert.
Why there are so much empty lines?

To make you understand what others tell you, all below solutions (only with row_number) are valid answers until you gave the rules (as I asked you):
SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2  FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY null) r
  3        FROM xxc_contact a)
  4  WHERE r = 1
  5  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Cell       9999999999
    123457 Daryl Dixon          FR         Work       7777777777
    123458 Maggie Greene        CH         Cell       5555555555

3 rows selected.

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2  FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY dbms_random.value) r
  3        FROM xxc_contact a)
  4  WHERE r = 1
  5  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Work       8888888888
    123457 Daryl Dixon          FR         Home       3333333333
    123458 Maggie Greene        CH         Home       6666666666

3 rows selected.

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2  FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY alternatecontact) r
  3        FROM xxc_contact a)
  4  WHERE r = 1
  5  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Cell       9999999999
    123457 Daryl Dixon          FR         Work       7777777777
    123458 Maggie Greene        CH         Cell       5555555555

3 rows selected.

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2  FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY alternatecontact desc) r
  3        FROM xxc_contact a)
  4  WHERE r = 1
  5  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Cell       9999999999
    123457 Daryl Dixon          FR         Work       7777777777
    123458 Maggie Greene        CH         Cell       5555555555

3 rows selected.

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2  FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY relation) r
  3        FROM xxc_contact a)
  4  WHERE r = 1
  5  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Cell       9999999999
    123457 Daryl Dixon          FR         Work       7777777777
    123458 Maggie Greene        CH         Cell       5555555555

3 rows selected.

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2  FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY relation desc) r
  3        FROM xxc_contact a)
  4  WHERE r = 1
  5  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Cell       9999999999
    123457 Daryl Dixon          FR         Work       7777777777
    123458 Maggie Greene        CH         Cell       5555555555

3 rows selected.

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2  FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY phtype) r
  3        FROM xxc_contact a)
  4  WHERE r = 1
  5  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Cell       9999999999
    123457 Daryl Dixon          FR         Home       3333333333
    123458 Maggie Greene        CH         Cell       5555555555

3 rows selected.

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2  FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY phtype desc) r
  3        FROM xxc_contact a)
  4  WHERE r = 1
  5  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Work       8888888888
    123457 Daryl Dixon          FR         Work       7777777777
    123458 Maggie Greene        CH         Home       6666666666

3 rows selected.

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2  FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY phone) r
  3        FROM xxc_contact a)
  4  WHERE r = 1
  5  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Work       8888888888
    123457 Daryl Dixon          FR         Home       3333333333
    123458 Maggie Greene        CH         Cell       5555555555

3 rows selected.

SQL> SELECT empnum, alternatecontact, relation, phtype, phone
  2  FROM (SELECT a.*, row_number() over(PARTITION BY empnum ORDER BY phone desc) r
  3        FROM xxc_contact a)
  4  WHERE r = 1
  5  /
    EMPNUM ALTERNATECONTACT     RELATION   PHTYPE          PHONE
---------- -------------------- ---------- ---------- ----------
    123456 Rick Grimes          SP         Cell       9999999999
    123457 Daryl Dixon          FR         Work       7777777777
    123458 Maggie Greene        CH         Home       6666666666

3 rows selected.


Re: SQL query to retrieve one record for each employee [message #620311 is a reply to message #620310] Thu, 31 July 2014 02:42 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Thanks Michel..
Quote:

Your test case is still invalid: no ";" at the end of insert.
Why there are so much empty lines?

That is my mistake.
Quote:

To make you understand what others tell you, all below solutions (only with row_number) are valid answers until you gave the rules (as I asked you):

Yes,I need the output , an order by phone
Re: SQL query to retrieve one record for each employee [message #620321 is a reply to message #620311] Thu, 31 July 2014 03:13 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
Yes,I need the output , an order by phone


So take the last but one (or use MIN).

Re: SQL query to retrieve one record for each employee [message #620342 is a reply to message #620321] Thu, 31 July 2014 07:12 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
For cross-reference https://community.oracle.com/thread/3590535?sr=inbox
Re: SQL query to retrieve one record for each employee [message #620367 is a reply to message #620342] Thu, 31 July 2014 14:23 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
The Walking Dead, eh?
Re: SQL query to retrieve one record for each employee [message #620375 is a reply to message #620367] Thu, 31 July 2014 15:02 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
For zombies who still got something alive, perhaps I am one of them. As a quick reference. Sort of Pingback

[Updated on: Thu, 31 July 2014 15:22]

Report message to a moderator

Previous Topic: Oracle server address
Next Topic: Production Query
Goto Forum:
  


Current Time: Fri Apr 26 04:39:20 CDT 2024