Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multiple Max Statements

Posted on 2016-09-16
11
Medium Priority
?
40 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 49

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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

705 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