Jump to content

Recommended Posts

I am currently having a problem getting the exact query for my problem.  I have a table that has the following columns:

 

customer, date, data1, data2, data3

I'm looking to create a query that gives me:

 

a unique customer entry

a unique date entry (where the date is the most recent entry prior than a given date)

data1, data2, and data3 (ordered by one of these columns)

 

My problem is that I can't seem to get the data ordered by the non-distinct columns.  I end up with something like this:

 

SELECT data1, data2, data3 FROM custtable WHERE date <= 04/15/2010 GROUP BY customer ORDER BY date DESC

 

The problem with the above query is that I am only able to sort by the date (in order to get only the most recent date entry) and am unable to sort by the columns that I'm actually querying for.

 

The goal of this query is to select the most recent entry prior to a given date for all unique customers and be able to sort the data in those entries.  Any help is much appreciated (I think I may need to use some HAVING or DISTINCT keywords, but I haven't been able to make it work).  Thanks!

Does your table have a unique id field? If so, perhaps a sub-query would work for you:

 

SELECT data1, data2, data3
FROM   custtable
WHERE  recordid in (
   SELECT recordid
   FROM   custtable
   WHERE  date <= '04/15/2010'
   GROUP  BY customer
   ORDER  BY date DESC)
ORDER  BY data1, data2, data3

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.