Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello all,

With the following query I am trying to pull some data from a table:
SQL
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
Updated 10-Jan-13 2:12am
v4
Comments
André Kraak 10-Jan-13 8:06am    
Please give the table structure, as it might influence the answer.
wonder-FOOL 10-Jan-13 8:13am    
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.

The best way to achieve that is to create stored procedure[^], like this one:

SQL
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:
SQL
EXEC YourDatabase.GetLatestDailyChange @client = N'00', @db = N'DBNAME', @class=N'CLASSNAME', @function=N'FUNCNAME';
 
Share this answer
 
SQL
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
 
Share this answer
 
Comments
wonder-FOOL 11-Jan-13 4:56am    
Thank you!
hey try below T-SQL Code :

SQL
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
 
Share this answer
 
Comments
wonder-FOOL 11-Jan-13 4:56am    
Thank you!
Bhushan Shah1988 11-Jan-13 4:58am    
mark as answer if it helps you.
Maciej Los 12-Jan-13 17:04pm    
ROWNUMBER() is good idea, my 5!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900