Solved

Multiple Max Statements

Posted on 2016-09-16
11
27 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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