473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Sql Max(date)

Pls, i need help.....
Otherwise i have to write my CV very soon...

PK : USER_ID,FROM_DATE

Each user has 3 rows. I need all users but only with the last row where FROM_DATE is the last
This is the basic SQL I need. Now….. Where can we put MAX(FROM_DATE )?


Expand|Select|Wrap|Line Numbers
  1. SELECT   USER_ID,
  2.                 to_char(FROM_DATE,'ddmmyyyy'),
  3.                 NVL(SALE_CHANNEL_CODE,0),
  4.                 NVL(PROFILE_CODE,0),
  5.                 USER_NAME, 
  6.                 NVL(DIVISION_CODE,0), 
  7.                 NVL(REGION_CODE,0),
  8.                 NVL( DEPARTMENT_CODE,0),
  9.                 NVL(STORE_NUM, 0),
  10.                 NVL(SECTION_CODE,0),
  11.                 NVL(ROLE_CODE,0),
  12.                 STATUS
  13. FROM  NAM_USERS_MANAGE
  14.  WHERE  DIVISION_CODE IN (1,3) AND USER_ID IN  ( SELECT  USER_ID
  15.                                                                    FROM  NAM_USERS )
  16.  
I took one example from the Internet :

Expand|Select|Wrap|Line Numbers
  1. SELECT t.EmployeeId, t.ReviewDate, t.ReviewRating
  2. FROM   tbl_stuff t
  3.        INNER JOIN (
  4.           SELECT EmployeeId, MAX(ReviewDate) AS MaxReviewDate
  5.           FROM tbl_stuff GROUP BY EmployeeId
  6.        ) t1 ON t.EmployeeId = t1.EmployeeId AND t.ReviewDate = t1.MaxReviewDate
  7. WHERE (some filters on t)
  8.  
But I've got "SQL Command was not properly ended"
So I took another one from the Internet:


Expand|Select|Wrap|Line Numbers
  1. SELECT P.*
  2. FROM (SELECT USER_ID, MAX(FROM_DATE) AS FROM_DATE
  3.       FROM NAM_USERS_MANAGE
  4.       GROUP BY USER_ID) AS O
  5. INNER JOIN
  6. NAM_USERS_MANAGE AS P
  7. ON P.USER_ID = O.USER_ID 
  8. P.FROM_DATE = O.FROM_DATE;
  9.  
And I've got "SQL Command was not properly ended" again.
So I thought maybe its related to SQL 2005 or something sp I took this one:

Expand|Select|Wrap|Line Numbers
  1. select EmployeeID, ReviewDate, ReviewRating
  2. from
  3.     (select EmployeeID, ReviewDate, ReviewRating, rank() over (partition by EmployeeID order by ReviewDate desc) as rank
  4.      from tbl_Stuff
  5.      where EmployeeID = X) a
  6. where rank = 1
  7.  
But it gives me all the data and I have no idea what is RANK...
May 5 '07 #1
2 13935
pradeep kaltari
102 Expert 100+
Pls, i need help.....
Otherwise i have to write my CV very soon...

PK : USER_ID,FROM_DATE

Each user has 3 rows. I need all users but only with the last row where FROM_DATE is the last
This is the basic SQL I need. Now….. Where can we put MAX(FROM_DATE )?


SELECT USER_ID,
to_char(FROM_DATE,'ddmmyyyy'),
NVL(SALE_CHANNEL_CODE,0),
NVL(PROFILE_CODE,0),
USER_NAME,
NVL(DIVISION_CODE,0),
NVL(REGION_CODE,0),
NVL( DEPARTMENT_CODE,0),
NVL(STORE_NUM, 0),
NVL(SECTION_CODE,0),
NVL(ROLE_CODE,0),
STATUS
FROM NAM_USERS_MANAGE
WHERE DIVISION_CODE IN (1,3) AND USER_ID IN ( SELECT USER_ID
FROM NAM_USERS )

I took one example from the Internet :

SELECT t.EmployeeId, t.ReviewDate, t.ReviewRating
FROM tbl_stuff t
INNER JOIN (
SELECT EmployeeId, MAX(ReviewDate) AS MaxReviewDate
FROM tbl_stuff GROUP BY EmployeeId
) t1 ON t.EmployeeId = t1.EmployeeId AND t.ReviewDate = t1.MaxReviewDate
WHERE (some filters on t)

But I've got "SQL Command was not properly ended"


So I took another one from the Internet:

SELECT P.*
FROM (SELECT USER_ID, MAX(FROM_DATE) AS FROM_DATE
FROM NAM_USERS_MANAGE
GROUP BY USER_ID) AS O
INNER JOIN
NAM_USERS_MANAGE AS P
ON P.USER_ID = O.USER_ID
P.FROM_DATE = O.FROM_DATE;

And I've got "SQL Command was not properly ended" again.
So I thought maybe its related to SQL 2005 or something sp I took this one:

select EmployeeID, ReviewDate, ReviewRating
from
(select EmployeeID, ReviewDate, ReviewRating, rank() over (partition by EmployeeID order by ReviewDate desc) as rank
from tbl_Stuff
where EmployeeID = X) a
where rank = 1

But it gives me all the data and I have no idea what is RANK...

Try the following:
Expand|Select|Wrap|Line Numbers
  1. SELECT   USER_ID,
  2.                 to_char(FROM_DATE,'ddmmyyyy'),
  3.                 NVL(SALE_CHANNEL_CODE,0),
  4.                 NVL(PROFILE_CODE,0),
  5.                 USER_NAME, 
  6.                 NVL(DIVISION_CODE,0), 
  7.                 NVL(REGION_CODE,0),
  8.                 NVL( DEPARTMENT_CODE,0),
  9.                 NVL(STORE_NUM, 0),
  10.                 NVL(SECTION_CODE,0),
  11.                 NVL(ROLE_CODE,0),
  12.                 STATUS
  13. FROM  NAM_USERS_MANAGE OUTER
  14.  WHERE  DIVISION_CODE IN (1,3) AND USER_ID IN  ( SELECT  USER_ID
  15.                                                                    FROM  NAM_USERS)
  16. AND DATE_FROM = (SELECT MAX(DATE_FROM) FROM NAME_USERS_MANAGE INNER
  17. WHERE OUTER.USER_ID=INNER.USER_ID )
  18. GROUP BY USER_ID
  19.  
I hope this solves your problem.
May 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: NBaker | last post by:
I have 1 table 'Data' and i need to select the max date. The table contains multiple rows per URN so I need all the fields where the date is most recent. URN £ Date Gift Type ...
1
by: 400NEWBIE | last post by:
Hi everyone, I'm having trouble figuring out how to select the max date in MS ACCESS from an AS400 table with a composite key. Here's my data: Type Name Select-Rule Error-Rule Effec-Date...
1
by: Freddie | last post by:
Hello everyone: i am trying to compare a MAX(DATE) from one table that would be greater than a date in another. first_table compname MYDATE abc comp 2006-09-26 09:19:43.250
9
by: robtyketto | last post by:
Is there anyway to change the max date to datenow + 2 years I tried this in the properties -> Format -> MaxDate = dateadd("yyyy",2,date()) without success :( Can anyone help. Thanks, a...
1
by: Matik | last post by:
Hello Everybody, I have a problem, with select stmt: SELECT TOP 15 * FROM oaVIEW_MainData AS TOP_VIEW, oaLanguageData_TAB AS RwQualifierJoin with (nolock) WHERE (c_dateTime>='2007.01.10...
1
by: Anne150585 | last post by:
Hi, I was wandering if anyone could give me some help... The idea is that I want to create a stored procedure that will check a bunch of tables and give me the latest 'LastUpdated' field ...
1
by: abetancur | last post by:
I have the following code: SELECT MAX(INVENTORY_TRANSACTION_HIST.DATE_APPLIED) AS LAST_TRANS, INVENTORY_TRANSACTION_HIST.PART_NO FROM INVENTORY_TRANSACTION_HIST INNER JOIN ...
3
Inbaraj
by: Inbaraj | last post by:
Hi... I have a Table in that i have inserted the DATE as VARCHAR now in that i Want to get the Max Date. I am new to MySQL Plz help me how to find the max date. with regard Inbaraj.D
5
by: sbettadpur | last post by:
hello Let me explain first the table structure. table - > pds fields - >District_ID, W_ID, Commodity_ID, Distribution_Cat_ID, Date, OB, Closing_Stock here i want the Closing_Stock value...
10
by: janieavis | last post by:
If I run this query I can see there are 2 records with the dates 10 August 2009 and 24 August 2009: SELECT dbo_JobMain.BpaMainPKey, dbo_JobMain.JobDefinitionPKey, dbo_JobMain.Value,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.