Solved

Multiple Max Statements

Posted on 2016-09-16
11
22 Views
Last Modified: 2016-09-16
I have the current code in my where clause:

and   prh.beg_date = (select max(prh.beg_date)          
          from prod.prratehist prh
          where prh.employee = emp.employee  )

Another criteria I need to put in is max(seq_nbr).
Can I put this in the order by clause?
If not, how else could I do this?
0
Comment
Question by:metalteck
  • 5
  • 5
11 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41802613
The existing small subquery can ONLY return a singe value, you ca'y just add another column

So you could add another similar small subquery, e.g.

and   prh.some_other_date = (select max(seq_nbr)
          from prod.prratehist prh
          where prh.employee = emp.employee  )

There may be a more efficient options than this, depends on what you are trying to achieve, include the full query perhaps.
0
 

Author Comment

by:metalteck
ID: 41802617
I tried using the following code:
and   prh.beg_date = (select max(prh.beg_date)
          from prod.prratehist prh
          where prh.employee = emp.employee)

and   prh.seq_nbr = (select max(prh.seq_nbr)
          from prod.prratehist prh
          where prh.employee = emp.employee )

This helped eliminate the duplicates that were being generated, but it also removed half of the expected results I had before adding the second subquery.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 41802620
Can you post some sample data with expected result?
0
 

Author Comment

by:metalteck
ID: 41802626
This is what I'm currently getting with only the first sub-query.
Ideally, I would like to retrieve all records that have the most current beg_date and if there are multiple seq_nbr, the highest one too.

Co   Emp        Beg_Date                     seq_nbr  pay_rate  annual salary  obj_id   date Stame
1      10901      08/21/2016 00:00:00      2             26              54080                596105      08/24/2016 00:00:00
1      10901      08/21/2016 00:00:00      0             26              54080                0               08/22/2016 00:00:00
1      10901      08/21/2016 00:00:00      1             19.25      40040                595695      08/22/2016 00:00:00
0
 
LVL 40

Expert Comment

by:Sharath
ID: 41802637
What is your SQL Server version? if it supports ROW_NUMBER, you can try like this.
;WITH cte AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY employee ORDER BY Beg_Date DESC, seq_nbr DESC) rn
FROM prod.prratehist)
SELECT *
FROM cte
WHERE rn = 1

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 40

Expert Comment

by:Sharath
ID: 41802639
If you want to use the subqueries, you can try like this.
and   prh.beg_date = (select max(prh.beg_date)
          from prod.prratehist prh
          where prh.employee = emp.employee)

and   prh.seq_nbr = (select max(prh.seq_nbr)
          from prod.prratehist prh
          where prh.employee = emp.employee  and prh.beg_date = emp.beg_date)

Open in new window

0
 

Author Comment

by:metalteck
ID: 41802642
@Sharath,
I'm using Oracle and on the subquery example you gave, emp.beg_date does not exisit.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 41802645
Can you post your complete query? Oracle supports ROW_NUMBER. you can try that too.
0
 

Author Comment

by:metalteck
ID: 41802649
select *
 from PROD.EMPLOYEE emp,
      PROD.PAEMPLOYEE pem,
      PROD.BENEFIT ben,
      PROD.PRRATEHIST prh,
      PROD.PAPOSITION pos,
      PROD.HRSUPER hsu,
      PROD.EMPLOYEE emp1,
      PROD.ZZWEBUSER zzw,
      PROD.PCODES pco

where emp.company = pem.company
and   emp.employee = pem.employee
and   emp.company = ben.company
and   emp.employee = ben.employee
and   emp.company = prh.company
and   emp.employee = prh.employee
and   emp.company = pos.company
and   emp.position = pos.position
and   emp.company = hsu.company
and   emp.supervisor = hsu.code
and   hsu.company = emp1.company
and   hsu.employee = emp1.employee
and   zzw.company = hsu.company
and   upper(zzw.userid) = hsu.user1
and   pem.locat_code = pco.code
and   emp.emp_status not in('T','TP','SR','LG','LU')
and   ben.plan_code in ('LTD','LTD1','LTD2')  
AND   ben.STOP_DATE=TO_DATE ('01-01-1700 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
and   prh.beg_date = (select max(prh.beg_date)
          from prod.prratehist prh
          where prh.employee = emp.employee)
and   prh.seq_nbr = (select max(prh.seq_nbr)
          from prod.prratehist prh
          where prh.employee = emp.employee )
and   pos.effect_date = (select max(pos.effect_date)
          from prod.paposition pos
          where pos.position = emp.position)
         
order by emp.employee
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 41802652
try this.
select *
 from PROD.EMPLOYEE emp,
      PROD.PAEMPLOYEE pem,
      PROD.BENEFIT ben,
      PROD.PRRATEHIST prh,
      PROD.PAPOSITION pos,
      PROD.HRSUPER hsu,
      PROD.EMPLOYEE emp1,
      PROD.ZZWEBUSER zzw,
      PROD.PCODES pco

where emp.company = pem.company
and   emp.employee = pem.employee
and   emp.company = ben.company
and   emp.employee = ben.employee
and   emp.company = prh.company 
and   emp.employee = prh.employee
and   emp.company = pos.company
and   emp.position = pos.position
and   emp.company = hsu.company
and   emp.supervisor = hsu.code
and   hsu.company = emp1.company 
and   hsu.employee = emp1.employee 
and   zzw.company = hsu.company
and   upper(zzw.userid) = hsu.user1
and   pem.locat_code = pco.code
and   emp.emp_status not in('T','TP','SR','LG','LU')
and   ben.plan_code in ('LTD','LTD1','LTD2')  
AND   ben.STOP_DATE=TO_DATE ('01-01-1700 00:00:00', 'DD-MM-YYYY HH24:MI:SS') 
and   prh.beg_date = (select max(prh1.beg_date)
          from prod.prratehist prh1
          where prh1.employee = emp.employee)
and   prh.seq_nbr = (select max(prh1.seq_nbr)
          from prod.prratehist prh1
          where prh1.employee = emp.employee and prh.beg_date = prh1.beg_date)
and   pos.effect_date = (select max(pos1.effect_date)
          from prod.paposition pos1
          where pos1.position = emp.position)
          
order by emp.employee

Open in new window

0
 

Author Closing Comment

by:metalteck
ID: 41802663
Sharath, that was exactly what I needed. Thanks for all your help!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now