catchingup
Technical User
I am trying to use the MAX function to get the most recent value for a column and not having success using the following formula:
SELECT DISTINCT COL3, COL2, COL20
FROM VALUETABLE AS VF
WHERE COL20 =
(SELECT MAX(COL20)
FROM VALUETABLE
WHERE COL2 = VF.COL2)
ORDER BY COL3, COL2
The values I am returning by this statement are inaccurate.
If you were to look at the table - VALUETABLE - as is, it looks like the following:
COL3 COL2 COL20
JC FGH 2007-06-30
JC FGH 2006-05-31
JC FGH 2004-08-31
JC MCW 2007-02-30
JC MCW 2007-06-30
TG BT 2006-11-30
TG BT 2007-06-30
TG FGH 2007-03-31
TG MCW 2004-08-31
TG MCW 2007-11-30
TG MCW 2005-02-28
FH ED 2003-04-01
What I am looking to get is a value for COL3 (which may be duplicated depending upon the number of associated entries in COL2); one value for COL2 and the max value of COL20 that goes along with COL2.
So my result set for the example above would be:
COL3 COL2 COL20
JC FGH 2007-06-30
JC MCW 2007-06-30
TG BT 2007-06-30
TG FGH 2007-03-31
TG MCW 2007-11-30
FH ED 2003-04-01
Any help is greatly appreciated!
Thank you
SELECT DISTINCT COL3, COL2, COL20
FROM VALUETABLE AS VF
WHERE COL20 =
(SELECT MAX(COL20)
FROM VALUETABLE
WHERE COL2 = VF.COL2)
ORDER BY COL3, COL2
The values I am returning by this statement are inaccurate.
If you were to look at the table - VALUETABLE - as is, it looks like the following:
COL3 COL2 COL20
JC FGH 2007-06-30
JC FGH 2006-05-31
JC FGH 2004-08-31
JC MCW 2007-02-30
JC MCW 2007-06-30
TG BT 2006-11-30
TG BT 2007-06-30
TG FGH 2007-03-31
TG MCW 2004-08-31
TG MCW 2007-11-30
TG MCW 2005-02-28
FH ED 2003-04-01
What I am looking to get is a value for COL3 (which may be duplicated depending upon the number of associated entries in COL2); one value for COL2 and the max value of COL20 that goes along with COL2.
So my result set for the example above would be:
COL3 COL2 COL20
JC FGH 2007-06-30
JC MCW 2007-06-30
TG BT 2007-06-30
TG FGH 2007-03-31
TG MCW 2007-11-30
FH ED 2003-04-01
Any help is greatly appreciated!
Thank you