1

I'm trying to modify a current SQL code in Microsoft Query I created to return the most recent production test conducted on a well. Right now the query below returns a list of tests for any well from [Date Parameter=01/01/08] to now for [Well Parameter= Well#1]. Also, to get all the data I need, I must join two tables, one with well names, one with production data, and both contain a unique well # which I join.

SELECT 
P.TEST_DT, P.BOPD, P.BWPD, P.MCFD
FROM 
WELL_TABLE C, PRODTEST_TABLE P
WHERE 
C.UNIQUE_WELL_ID = P.UNIQUE_WELL_ID 
AND ((C.WELL_NAME=?) 
AND (P.TEST_DT>=?))
ORDER BY P.TEST_DT DESC

Right now my table looks as such:

TEST_DT    P.BOPD  P.BWPD  P.MCFD
9/23/2012  23      125    0
8/23/2010  21      137    0
7/15/2009  29      123    0

I would like to just return the most recent test:

TEST_DT    P.BOPD  P.BWPD  P.MCFD
9/23/2012  23      125     0

Any help would be greatly appreciated... I've tried working with max(TEST_DT) but have been unsuccessful.

RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262

1 Answers1

0
SELECT tab.* 
  FROM (SELECT P.TEST_DT, P.BOPD, P.BWPD, P.MCFD
          FROM  WELL_TABLE C, PRODTEST_TABLE P
         WHERE C.UNIQUE_WELL_ID = P.UNIQUE_WELL_ID 
           AND C.WELL_NAME=?
         ORDER BY P.TEST_DT DESC) tab
 WHERE ROWNUM=1;
Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77