Solved

Need help with a query

Posted on 2016-09-02
8
50 Views
Last Modified: 2016-09-02
Hi Experts,

I need to find the duplicate records when the date and time is exactly same for 'StdTimeIn'. Sometimes the millisecond is different. But if the date and hh:mm:ss is same then I need those records. I have attached the data.

I want the records-
473      105      2016-08-31 15:19:56.767      2016-08-31 20:00:00.110      08312016
474      105      2016-08-31 15:19:56.767      2016-08-31 20:00:00.110      08312016

476      534      2016-08-31 16:22:23.537      2016-08-31 16:23:52.320      08312016
478      534      2016-08-31 16:22:23.540      2016-08-31 16:23:51.633      08312016
477      534      2016-08-31 16:22:23.537      2016-08-31 16:23:51.357      08312016

Thanks in advance.
Duplicates.txt
0
Comment
Question by:RadhaKrishnaKiJaya
8 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Where is the data coming from a file, EF query, DataTable or other??
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
SELECT StdLogID, UserID, StdTimeIn, StdTimeout, TodaysDate
FROM TableName T1
WHERE EXISTS (SELECT 1
        FROM TableName T2
        WHERE T1.UserID=T2.UserID
         AND T1.StdTimeIn=T2.StdTimeIn
         AND T1.StdTimeout=T2.StdTimeOut
         AND T1.TodaysDate=T2.TodaysDate
         AND T1.StdLogID<>T2.StdLogID)

Open in new window

0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
They are in a table in SQL server.
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
For starters, eyeballleth thy article SQL Server Delete Duplicate Rows Solutions, which is a sample code and image-rich tutorial on how to pull that off.

Applied to your code though <air code, holy cow make a backup first>..
;with cte as (
SELECT UserID, StdTimeIn, 
   ROW_NUMBER() OVER (PARTITION BY UserId, convert(varchar, StdTimeIn ,120) ORDER BY (SELECT NULL)) as row_number
FROM your_table) 
DELETE FROM cte WHERE row_number > 1

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 total points
Comment Utility
I created a temporary table #t, so replace that with your actual table name
select * from #t where convert(varchar,StdTimeIn,120) in (select convert(varchar,StdTimeIn,120) dy from #t group by convert(varchar,StdTimeIn,120) having count(*)>1)

Open in new window

0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
Hi Mr. Vitor,

I am getting the records using your query. But I do not want to match the miliseconds.  I want to check for todays date,  where StdTimeIn should same tiill yyyy-mm-dd hh:mm:ss. The millisecond might be different.

Thanks for your help.
0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
Comment Utility
Thank you very much. It worked!!
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
for info only:
an alternative that will list all rows that share the the same date/time measured to seconds.

SELECT
      *
FROM (
      SELECT
            *
          , COUNT(*) OVER(PARTITION BY CONVERT(varchar, StdTimeIn, 120) ) AS count_of
      FROM your_table
      ) D
WHERE count_of > 1

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now