Solved

Multiple Max Statements

Posted on 2016-09-16
11
29 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
length of the password hash sha1:64000 to set sql field property. 13 66
Loops and updating in SQL Query 9 55
How to use three values with DATEDIFF 3 35
awk and Pythagoras? 5 19
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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