Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7576721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:59:25+00:00 2026-05-30T16:59:25+00:00

I have a table of orders that I know have duplicates customer order_number order_date

  • 0

I have a table of orders that I know have duplicates

    customer   order_number   order_date
   ----------  ------------   -------------------
          1             1     2012-03-01 01:58:00
          1             2     2012-03-01 02:01:00
          1             3     2012-03-01 02:03:00
          2             4     2012-03-01 02:15:00
          3             5     2012-03-01 02:18:00
          3             6     2012-03-01 04:30:00
          4             7     2012-03-01 04:35:00
          5             8     2012-03-01 04:38:00
          6             9     2012-03-01 04:58:00
          6            10     2012-03-01 04:59:00

I want to find all duplicates (order by same customer within 60 minutes of eachother). Either a resultset consisting of the ‘duplicate’ rows or a set of all customers with a count of how many duplicates.

Here is what I have tried

SELECT
   customer,
   count(*)
FROM
   orders
GROUP BY
   customer,
   DATEPART(HOUR, order_date)
HAVING (count(*) > 1)

This doesn’t work when duplicates are within 60 minutes of each other but are in different hours i.e 1:58 and 2:02

I’ve also tried this

SELECT
  o1.customer,
  o1.order_number,
  o2.order_number,
  DATEDIFF(MINUTE,o1.order_date, o2.order_date) AS [diff]
FROM
  orders o1 LEFT OUTER JOIN
  orders o2 ON o1.customer = o2.customer AND o1.order_number <> o2.order_number
WHERE
  ABS(DATEDIFF(MINUTE,o1.order_date, o2.order_date)) < 60

Now this gives me all of the duplicates but it also gives me multiple rows per duplicate order. i.e (o1, o2) and (o2, o1) which wouldn’t be so bad if there were’nt some orders with multiple duplicates. In those cases I get (o1, o2), (o1,o3), (o2, o1), (o2, o3), (o3, o1), (o3, o2) etc. I get all of the permutations.

Anyone have some insight? I’m not necessarily looking for the best performing answer here, just one that works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:59:26+00:00Added an answer on May 30, 2026 at 4:59 pm
    SELECT
      *,
      CASE WHEN EXISTS (SELECT *
                          FROM orders AS lookup
                         WHERE customer    = orders.customer
                           AND order_date <  orders.order_date
                           AND order_date >= DATEADD(hour, -1, order_date)
                       )
           THEN 'Principle Order'
           ELSE 'Duplicate Order'
      END as Order_Status
    FROM
      orders
    

    Using EXISTS and a correlated sub-query you can check if there were any preceding orders in the last hour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ecommerce MySQL database that stores order information in the table 'orders'
If I have a Customers table linked to an Orders table, and I want
I have a customers table, and an orders table. Each customer can have many
I have a SQL Server table full of orders that my program needs to
I have one table orders with a foreing key ProductID. I want to show
I have a simple query like so: SELECT * FROM orders that returns all
I have a table that looks like: Client_ID | Order_ID | 10 | 1
Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
If I have a table Orders with fields CustomerID , OrderID and OrderDate ,
I have an orders table with a schema like this. CREATE TABLE orders (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.