Click here to Skip to main content
Sign Up to vote bad
good
Hello all,
 
With the following query I am trying to pull some data from a table:
SELECT c.CHANGEDBY, c.CODE, c.CHANGEDAT
FROM logs c
WHERE c.CLIENT='00' AND c.DB='DBNAME' AND c.CLASS='CLASSNAME' AND c.FUNCTION='FUNCNAME'
 
The table consists of the following columns
ID(int), DB(varchar), CLIENT(varchar), CLASS(varchar),FUNCTION(varchar), CODE(varchar), CREATEDBY(varchar), CREATEDAT(datetime), CHANGEDBY(varchar), CHANGEDAT(datetime)
 
When I run this query I have the output:
CHANGEDBY      CODE    CHANGEDAT
John-----------xyz-----2012-07-09 10:04:00
Ben------------abc-----2012-07-09 08:04:00
Walter---------def-----2012-07-08 07:15:00
Kate-----------ghf-----2012-07-08 11:35:00
------------------------------------------------------------------
What I want is I need the latest datetime for each day in this select query. So the output should be like:
CHANGEDBY      CODE    CHANGEDAT
John-----------xyz-----2012-07-09 10:04:00
Kate-----------ghf-----2012-07-08 11:35:00
When I use MAX(Date) it only brings 1 so can someone help me figure it out.
Thanks
Posted 10 Jan '13 - 1:47
Edited 10 Jan '13 - 2:12

Comments
André Kraak - 10 Jan '13 - 8:06
Please give the table structure, as it might influence the answer.
wonder-FOOL - 10 Jan '13 - 8:13
I have added the columns that table has. I will be working on the same table so there is no need to join external tables.

3 solutions

hey try below T-SQL Code :
 
CREATE TABLE #TEMP
(
    ID  INT,
    CHANGEDATE  DATETIME
)
 
INSERT INTO #TEMP VALUES(1,GETDATE())
 
INSERT INTO #TEMP VALUES(2,'2013-01-09 10:04:00')
 
INSERT INTO #TEMP VALUES(4,GETDATE())
 
INSERT INTO #TEMP VALUES(3,'2013-01-09 08:11:00')
 
SELECT DISTINCT ID, CHANGEDATE
FROM
(
    SELECT ID,CHANGEDATE,ROW_NUMBER() OVER(PARTITION BY CONVERT(VARCHAR(10),CHANGEDATE,103) ORDER BY CHANGEDATE DESC) AS ROWNUM
    FROM #TEMP
) TMP
WHERE ROWNUM = 1
 
DROP TABLE #TEMP
  Permalink  
Comments
wonder-FOOL - 11 Jan '13 - 4:56
Thank you!
Bhushan Shah1988 - 11 Jan '13 - 4:58
mark as answer if it helps you.
Maciej Los - 12 Jan '13 - 17:04
ROWNUMBER() is good idea, my 5!
SELECT C.ChangedBy, C.Code, C.ChangeDate
FROM dbo.logs c WITH(NOLOCK)
WHERE C.ChangeDate IN
(
SELECT MAX(C.ChangeDate)
                FROM dbo.logs c WITH(NOLOCK)
                WHERE CONVERT(DATE, , C.ChangeDate) = CONVERT(DATE, , C.ChangeDate)
                GROUP CONVERT(DATE, , C.ChangeDate)
)
AND c.CLIENT = ‘00’
AND c.DB = ‘DBNAME’
AND c.CLASS = ‘CLASSNAME’
AND c.FUNCTION = ‘FUNCNAME’
try this, you will get the data
  Permalink  
Comments
wonder-FOOL - 11 Jan '13 - 4:56
Thank you!
The best way to achieve that is to create stored procedure[^], like this one:
 
USE  YourDatabase;
GO
 
CREATE PROCEDURE GetLatestDailyChange
    @client VARCHAR(10),
    @db VARCHAR(30),
    @class VARCHAR(30),
    @function (30)
 
AS
    SELECT CHANGEDBY, CODE, CHANGEDAT
    FROM(
        SELECT ROW_NUMBER() OVER(PARTITION BY CHANGEDATE ORDER BY CHANGEDATE DESC) AS ID, * 
        FROM logs
        WHERE CLIENT=@client AND T.DB=@db AND T.CLASS=@class AND T.FUNCTION=@functions) AS T
    WHERE T.ID = 1
GO
 
More about CREATE PROCEDURE[^] commad.
 

After SP creation, you need to know how to call it. For example:
EXEC YourDatabase.GetLatestDailyChange @client = N'00', @db = N'DBNAME', @class=N'CLASSNAME', @function=N'FUNCNAME';
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 428
1 OriginalGriff 291
2 Mayur_Panchal 153
3 Mohammed Hameed 145
4 Dave Kreskowiak 125
0 Sergey Alexandrovich Kryukov 8,146
1 OriginalGriff 6,236
2 CPallini 3,482
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 12 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid