Solved

Need help with a query

Posted on 2016-09-02
8
78 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 63

Expert Comment

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

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

623 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