Get only the Maximum value directly from source

Skovgaard

Board Regular
Joined
Oct 18, 2013
Messages
204
Office Version
  1. 365
Platform
  1. Windows
Hi Experts,

Hope you can help.
In below picture, I'm trying to filter the field DXFVDT directly in the source, to only return the maximum value.

1665665513838.png


When I try with below code, only the headlines is returned, no values.
Can anybody help with this?

SQL:
SELECT OGDMTX.DXFVDT, OGDMTX.DXOBV1

FROM M3.M3FDBPRD.OGDMTX OGDMTX

WHERE (OGDMTX.DXDISY='DA') AND (OGDMTX.DXOBV1='42850050') AND (OGDMTX.DXFVDT = (SELECT MAX(OGDMTX.DXFVDT) FROM M3.M3FDBPRD.OGDMTX OGDMTX))


/Skovgaard
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I'd imagine you need a WHERE clause in the SELECT MAX part too.
 
Upvote 0
I'd imagine you need a WHERE clause in the SELECT MAX part too.

Well...
Below code gives me only the maximum as wanted, but as soon as I try to implement another filter, e.g. OGDMTX.DXOBV1='42850050', it gives an empty table.
How should the AND function be build together with the SELECT MAX?

SQL:
SELECT OGDMTX.DXFVDT, OGDMTX.DXOBV1

FROM M3.M3FDBPRD.OGDMTX OGDMTX

WHERE OGDMTX.DXFVDT = (SELECT MAX(OGDMTX.DXFVDT) FROM M3.M3FDBPRD.OGDMTX OGDMTX)

/Skovgaard
 
Upvote 0
Do you have an entry for 42850050 that matches the max value of the whole table?
 
Upvote 0
Do you have an entry for 42850050 that matches the max value of the whole table?

Argh... Rookie mistake 🙈🙈 Still in early SQL learning stage... 😊

Below code works...

SQL:
SELECT OGDMTX.DXFVDT, OGDMTX.DXOBV1

FROM M3.M3FDBPRD.OGDMTX OGDMTX

WHERE (OGDMTX.DXOBV1='110') AND (OGDMTX.DXFVDT = (SELECT MAX(OGDMTX.DXFVDT) FROM M3.M3FDBPRD.OGDMTX OGDMTX))

Is it possible to re-write the code, so I find the max value for entry 42850050?
I think...
Step 1: Find all 42850050
Sted 2: Find the max value

/Skovgaard
 
Upvote 0
Depending on what your data source is, you could do something like ordering the results in descending date order and then taking the first result - for example:

SQL:
SELECT TOP 1 OGDMTX.DXFVDT, OGDMTX.DXOBV1

FROM M3.M3FDBPRD.OGDMTX OGDMTX

WHERE (OGDMTX.DXDISY='DA') AND (OGDMTX.DXOBV1='42850050') ORDER BY OGDMTX.DXFVDT DESC
 
Upvote 0
Depending on what your data source is, you could do something like ordering the results in descending date order and then taking the first result - for example:

SQL:
SELECT TOP 1 OGDMTX.DXFVDT, OGDMTX.DXOBV1

FROM M3.M3FDBPRD.OGDMTX OGDMTX

WHERE (OGDMTX.DXDISY='DA') AND (OGDMTX.DXOBV1='42850050') ORDER BY OGDMTX.DXFVDT DESC

This gives me below error:
I appreciate your help!

1665734560972.png


/Skovgaard
 
Upvote 0
I'm not familiar with that data source, but based on a quick google you might need something like:

SQL:
SELECT OGDMTX.DXFVDT, OGDMTX.DXOBV1
FROM M3.M3FDBPRD.OGDMTX OGDMTX
WHERE (OGDMTX.DXDISY='DA') AND (OGDMTX.DXOBV1='42850050') ORDER BY OGDMTX.DXFVDT DESC
fetch first 1 row only
 
Upvote 0
I'm not familiar with that data source, but based on a quick google you might need something like:

SQL:
SELECT OGDMTX.DXFVDT, OGDMTX.DXOBV1
FROM M3.M3FDBPRD.OGDMTX OGDMTX
WHERE (OGDMTX.DXDISY='DA') AND (OGDMTX.DXOBV1='42850050') ORDER BY OGDMTX.DXFVDT DESC
fetch first 1 row only
I've testet, and it only gives me one row, the maximum date is in many rows.

I've also google through a lot of SQL functions and tried many things. Below is what I finally constructed, which works as intended.

SQL:
SELECT OGDMTX.DXFVDT, OGDMTX.DXOBV1

FROM M3.M3FDBPRD.OGDMTX OGDMTX
WHERE (OGDMTX.DXDISY='DA' AND OGDMTX.DXOBV1='42850050')
AND OGDMTX.DXFVDT = (SELECT MAX(OGDMTX.DXFVDT)
FROM M3.M3FDBPRD.OGDMTX OGDMTX
WHERE OGDMTX.DXDISY='DA' AND OGDMTX.DXOBV1='42850050')

Any way, thanks Rory for guiding me through this educational experience 😉👍

/Skovgaard
 
Upvote 0
Solution

Forum statistics

Threads
1,223,996
Messages
6,175,853
Members
452,675
Latest member
duongtruc1610

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top