Get the data of only latest date and time stamp from SQL Query.

Shreyas Kale 61 Reputation points
2022-05-01T09:12:19.82+00:00

I am creating PowerBI dashboard using SQL Database of attendance system. In dashboard I want to show employees count on each floor. (For Example - If employee punch-in on 6th floor attendance machine then employee count of 6th floor will increase by 1 number.

For this I have created new table in SQL database & written below query.

Select [UserID]
,[Username]
,[Edatetime]
,[DeviceName]
,[INOuttype]
From [PowerBI].[dbo].[PowerBI_data] order by edatetime desc

Currently whenever I execute this query output result is showing multiple IN & Out entries as employees Punch-In & Punch-out many times in a day.

Instead of showing all the IN & Out entries, I wanted to see ONLY RECENT Punch-In/Punch-Out entry results of each employee.

Can you please suggest what query I have write to get this data..?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,702 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 100.9K Reputation points MVP
    2022-05-01T09:35:19.743+00:00

    First of all, for a question like this, it is always a good idea to post the CREATE TABLE statement for your table(s) and INSERT statements with sample data together with the desired result given the sample. This makes it easy to copy and paste into a query window to develop a tested query.

    What you have below is an entirely untested query.

    SELECT U.UserID, U.UserName, P.Edatetime, P.DeviceName, P.InOutType
    FROM   Users U
    CROSS APPLY (SELECT TOP (1) P.Edatetime, P.DeviceName, P.InOutType
                 FROM  PowerBI_data P
                 WHERE P.UserID = U.UserID
                 ORDER BY P.Edatetime DESC) AS P
    

    If you don't have a Users table, you can replace FROM Users with

    FROM  (SELECT DISTINCT UserID, UserName FROM PowerBI_data) AS U
    

4 additional answers

Sort by: Most helpful
  1. Shreyas Kale 61 Reputation points
    2022-05-01T09:55:37.323+00:00

    197979-capture.png

    As you can see in table, whenever I execute query, it shows multiple records for each employee as they have puch-in and punch-out multiple times in a day.

    Instead of this, I want to get the details of only latest Punch-in or Punch-out.


  2. Naomi 7,361 Reputation points
    2022-05-01T20:11:57.953+00:00

    Do you want 2 rows for each employee (one for punch in, one for punch out). If yes, it would be small modification of Erland's query:

        ;with cte1 as (SELECT U.UserID, U.UserName, P.Edatetime as InTime, P.DeviceName as InDevice, P.InOutType
        FROM   Users U
        OUTER APPLY (SELECT TOP (1) P.Edatetime, P.DeviceName, P.InOutType
                     FROM  PowerBI_data P
                     WHERE P.UserID = U.UserID and InOutType = 1
                     ORDER BY P.Edatetime DESC) AS P),
    cte2 as ( (SELECT U.UserID, U.UserName, X.Edatetime as OutTime, P.DeviceName as OutDevice, P.InOutType
        FROM   Users U
        OUTER APPLY (SELECT TOP (1) P.Edatetime, P.DeviceName, P.InOutType
                     FROM  PowerBI_data P
                     WHERE P.UserID = U.UserID and InOutType = 0
                     ORDER BY P.Edatetime DESC) AS X)
    select ISNULL(c1.UserID, c2.UserID) as UserId, isnull(c1.UserName, c2.UserName) as UserName, c1.InTime, c1.InDevice, c2.OutTime, c2.OutDevice
    from cte1 c1 FULL JOIN cte2 c2 ON C1.UserId = c2.UserId
    
    0 comments No comments

  3. Bert Zhou-msft 3,421 Reputation points
    2022-05-02T06:37:46.037+00:00

    Hi,@Shreyas Kale
    Welcome to Microsoft T-SQL Q&A Forum!
    Please check this:

    SELECT TOP (1) P.Edatetime, U.UserID, P.DeviceName, P.InOutType  
    FROM   Users U inner join  PowerBI_data P on P.UserID = U.UserID  
    WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, Edatetime)) = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))  
    order by P.Edatetime Desc  
    

    Best regards,
    Bert Zhou


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Isabellaz-1451 3,616 Reputation points
    2022-05-03T09:22:02.72+00:00

    Hi @Shreyas Kale
    Please check this,table is like this:

    198521-image.png

    ;WITH CTE AS(select USEID,USERNAME,EDATETIME,DEVICENAME,INOuttype,ROWNUM = ROW_NUMBER()OVER(PARTITION BY USEID,USERNAME ORDER BY EDATETIME DESC) from csvtable where EDATETIME BETWEEN ( select DATEADD(hour,12,cast(cast(getdate() as date) as datetime))  
    ) AND(  select DATEADD(hour,24,cast(cast(getdate() as date) as datetime))  
    ) )  
    SELECT * FROM CTE WHERE ROWNUM = 1  
    

    Result:
    198512-image.png

    Best Regards,
    Isabella

    0 comments No comments