Solved

Multiple Max Statements

Posted on 2016-09-16
11
31 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 41

Expert Comment

by:Sharath
ID: 41802620
Can you post some sample data with expected result?
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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 41

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
 
LVL 41

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 41

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 41

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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