tags:

views:

49

answers:

1

I do have this final query (I've tried a lot of them to make this work), which i'm not able to get it run.

 SELECT customerNo, rating, dateApproved
 FROM ['worksheet$']
 WHERE dateCreated = (SELECT MAX(dateCreated) FROM ['worksheet$'] )
 AND status = 'Approved'"

For a customerNo 0001 there are two or maybe more rows, and I want to get the row where the dateApproved is the maximum for that customerNo.

How can i achieve this? It's causing me a lot of headache. Already asked a few friends and also googled, no relevant answers found.

thanks in advance

+1  A: 

The "SELECT MAX(..." subquery is going to return the max date created for the ENTIRE worksheet; not just for the specific customer you care about.

I'm not sure if Excel will support this syntax, but in a normal database I'd suggest this query to get what you want:

 SELECT customerNo, rating, dateApproved
 FROM ['worksheet$'] wkOuter
 WHERE status = 'Approved'
   and dateCreated = (
   SELECT MAX(dateCreated)
   FROM ['worksheet$'] wkInner
   WHERE wkInner.customerNo = wkOuter.customerNo
   )
JosephStyons
no, the max(dateCreated) is fine, when I do this kind of query, where the dateApproved is maximum, it throws an exception in code, telling me that the wkOuter or wkInner is not found as an object, that's the point I get stuck
I'm looking for the correct syntax or some common way it is done, cause logically i do know how it should work