Solved

Need help with a query

Posted on 2016-09-02
8
63 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
ID: 41781752
Where is the data coming from a file, EF query, DataTable or other??
0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 41781756
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
ID: 41781757
They are in a table in SQL server.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 41781758
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
 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 total points
ID: 41781788
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
ID: 41781926
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
ID: 41781999
Thank you very much. It worked!!
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41782442
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

776 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