Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with a query

Posted on 2016-09-02
8
Medium Priority
?
85 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41781752
Where is the data coming from a file, EF query, DataTable or other??
0
 
LVL 52

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 66

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 2000 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 49

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

688 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