>One customer could have several records in the same date and I have to 
make a SELECT only to the first table_id for a given date for
 >each customer_id. I have worked around the query with something like 
this but I haven't succeed:

Hi Hernando!

Admittedly I have tried to guess what you mean by the above statement (I 
didn't understand anything of what you want from the query itself - 
except that you only want the result for tomorrow). If my guess is 
correct, you simply want:

SELECT cast(date_time as Date) mydate, customer, min(table_id) table_id
FROM my_table
WHERE date_time >= current_date + 1
   AND date_time < current_date + 2
GROUP BY 1, 2

A different way to write something similar, would be:
SELECT xx0.table_id
FROM my_table xx0
WHERE xx0.date_time >= current_date + 1
   AND xx0.date_time < current_date + 2
   AND NOT EXISTS(SELECT *
                  FROM my_table xx1
                  WHERE XX0.customer = XX1.customer
                    AND CAST(XX0.date_time AS DATE) = CAST(XX1.date_time 
AS DATE)
                    AND XX0.table_id > XX1.table_id)

(you may add AND XX1.date_time between XX0.date_time - 1 and 
XX0.date_time + 1 if you have an index for date_time and your query is slow)

If this is not what you're looking for, then please try to explain your 
problem in the way you would explain it to someone that doesn't know 
anything about your system - that could include an example dataset and 
an example of the desired output.

HTH,
Set

Reply via email to