We help IT Professionals succeed at work.

SQL query to select row with MAX date

6,450 Views
Last Modified: 2017-04-05
I'm trying to return the row with the MAX date but the MAX funciton doesn't appear to be working. Below is the code I'm trying to run along with the outputMax-query.JPG: Any help you can provide will be very much appreciated.

select doc_prfx, doc_num, BKD_WGT, BKD_VOL, ACCEPTED_PCS, ACCEPTED_VOL, sum (ACCEPTED_WGT - BKD_WGT), sum (ACCEPTED_VOL - BKD_VOL), MAX(modified_date)

from 

(Select distinct 
c.car_id,
 b.doc_prfx, b.doc_num, 

f.wt_bfr_awb as BKD_WGT, f.vol_bfr_awb as BKD_VOL,

g.num_pcs as ACCEPTED_PCS,
g.ttl_wt as ACCEPTED_WGT, g.vol as ACCEPTED_VOL,

to_char(f.modified_date,  'DD-MON-YYYY HH24:MI:SS') as modified_date

from sbh_res_itnry a, sbh_car b, sbh_res c, sbh_res_cust d, fwb_res_data_aud e, sbh_res_aud f, sbh_AWB g
where c.car_id = b.car_id (+)
and c.car_id = a.car_id 
and c.car_id = d.car_id
and b.car_id = e.car_id (+)
and c.car_id = f.car_id 
and c.car_id = g.car_id
and a.aln_code = 'VS'
and a.flt_num = '0003'
and trunc (a.dep_date) = '04-APR-2017'
--and e.rmk_typ = 'BKL' --in (null,'BKL')
AND a.cncl_ind = 'N'
and d.cust_typ = 'AGT'
and b.cncl_ind = 'N'
and c.enq_ind = 'N'
and f.wt_bfr_awb is not null
and b.doc_prfx = '932'
and b.doc_num in ('51493422')
)
group by 

doc_prfx, doc_num, BKD_WGT, BKD_VOL, ACCEPTED_PCS, ACCEPTED_VOL,modified_date

Open in new window

Comment
Watch Question

ste5anSenior Developer
CERTIFIED EXPERT

Commented:
To filter by that, you need a subquery. The MAX() aggregate itself does only a calculation. The normal approach is:

SELECT *
FROM yourQuery
WHERE dateColumn = ( 
    SELECT MAX(dateColumn) 
    FROM yourQuery
);

Open in new window


Depending on the problem, the subquery must be correlated.
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
johnsoneSenior Oracle DBA
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
ste5anSenior Developer
CERTIFIED EXPERT

Commented:
Caveat: Test always both approaches. Cause ROW_NUMBER() is not necessarily always better.
Senior Oracle DBA
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
thanks for you help guys

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions