Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MAX(DATE) - SQL ORACLE

Tags:

sql

oracle11g

I want to select only the latest membership_id from table user_payments of the user with the user_id equal to 1.

This is how the table user_payment looks like:

   PAYM_ID    USER_ID MEMBSHIP_ID PAYM_DATE                     
---------- ---------- ----------- -------------------------------
         1          1           1 18-DEC-09 12.00.00.000000000 AM 
         2          1           2 18-DEC-10 12.00.00.000000000 AM 
         3          1           2 18-DEC-11 12.00.00.000000000 AM 
         4          2           3 17-MAR-11 12.00.00.000000000 AM 
         5          3           3 18-JUN-12 12.00.00.000000000 AM 
         6          4           2 17-FEB-12 12.00.00.000000000 AM 
         7          5           2 18-FEB-11 12.00.00.000000000 AM 
         8          5           2 18-FEB-12 12.00.00.000000000 AM 
         9          6           1 01-JUN-12 12.00.00.000000000 AM 
        10          7           1 03-FEB-11 12.00.00.000000000 AM 
        11          7           2 03-FEB-12 12.00.00.000000000 AM 

I am trying with no success the following code:

SELECT MEMBSHIP_ID
FROM user_payment
WHERE user_id=1 and MAX(paym_date);

And I get this error: SQL Error: ORA-00934: group function is not allowed here 00934. 00000 - "group function is not allowed here"

How can I fix it? thanks in advance!

like image 614
Cristy Avatar asked Jul 09 '12 07:07

Cristy


People also ask

What is the max date in Oracle?

Oracle Database can store dates in the Julian era, ranging from January 1, 4712 BCE through December 31, 9999 CE (Common Era, or 'AD').

Can you max a date in SQL?

MAX() function will give you the maximum values from all the values in a column. MAX function works with “date” data types as well and it will return the maximum or the latest date from the table.

How do you find the max effective date in SQL?

If you want a the max effdt even when the only effective date for the tree is future, simply remove the effdt/sysdate criteria entirely from the SQL. If you want the max effdt <= sysdate when there is an effdt <= sysdate but you want the min effdt when there are only future effdt, it gets more complicated.

How do I query the latest date in SQL?

Here is the syntax that we can use to get the latest date records in SQL Server. Select column_name, .. From table_name Order By date_column Desc; Now, let's use the given syntax to select the last 10 records from our sample table.


3 Answers

select * from 
  (SELECT MEMBSHIP_ID
   FROM user_payment WHERE user_id=1
   order by paym_date desc) 
where rownum=1;
like image 149
manurajhada Avatar answered Sep 29 '22 10:09

manurajhada


SELECT p.MEMBSHIP_ID
FROM user_payments as p
WHERE USER_ID = 1 AND PAYM_DATE = (
    SELECT MAX(p2.PAYM_DATE)
    FROM user_payments as p2
    WHERE p2.USER_ID = p.USER_ID
)
like image 42
shawnt00 Avatar answered Sep 29 '22 09:09

shawnt00


Try:

SELECT MEMBSHIP_ID
  FROM user_payment
 WHERE user_id=1 
ORDER BY paym_date = (select MAX(paym_date) from user_payment and user_id=1);

Or:

SELECT MEMBSHIP_ID
FROM (
  SELECT MEMBSHIP_ID, row_number() over (order by paym_date desc) rn
      FROM user_payment
     WHERE user_id=1 )
WHERE rn = 1
like image 41
A.B.Cade Avatar answered Sep 29 '22 11:09

A.B.Cade