Hello I have a table with columns:
*using oracle
ID NUMBER USER_ID NUMBER DATE_ADDED DATE DATE_VIEWED DATE DOCUMENT_ID VARCHAR2 URL VARCHAR2 DOCUMENT_TITLE VARCHAR2 DOCUMENT_DATE DATE
I want to know how i can get the most recently added document for a given user.
Select * FROM test_table WHERE user_id = value AND (do something with date_added column)
Thanks
We could use LAST_VALUE() in SQL Server to find the last value from any table. LAST_VALUE() function used in SQL server is a type of window function that results the last value in an ordered partition of the given data set.
SELECT name AS TableName, create_date AS CreatedDate, modify_date as ModifyDate FROM sys. tables order by ModifyDate; ...will tell me the last time a table was created and modified (from a DDL perspective).
Select * FROM test_table WHERE user_id = value AND date_added = (select max(date_added) from test_table where user_id = value)
Not sure of exact syntax (you use varchar2 type which means not SQL Server hence TOP) but you can use the LIMIT keyword for MySQL:
Select * FROM test_table WHERE user_id = value ORDER BY DATE_ADDED DESC LIMIT 1
Or rownum in Oracle
SELECT * FROM (Select rownum as rnum, * FROM test_table WHERE user_id = value ORDER BY DATE_ADDED DESC) WHERE rnum = 1
If DB2, I'm not sure whether it's TOP, LIMIT or rownum...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With