Dublicated rows by certain critera

I have rows in my Salestable where I want to find duplicated rows with different salesid.

F.ex. There is a transaction where CustomerKey, Quantity, AccountNumber is dublicated in the database.
The only thing that is not the same is the SalesOrderId but to know that there is a dublicated transaction,
the other SalesOrderID with the same CustomerKey, Quantity and Accountnumber are the same, but the transaction
createddatetime is within 5 minutes between those two transactions.

Any Idea how I could write my query to find all SalesOrderId where above criteria is matched?

f.ex.

SalesOrderID    Customerkey      AccountNumber        Quantity                  CreatedDateTime
5001                 250                                1100                  1350                        2015-08-19 09:16:31.000
5002                 250                                 1100                  1350                     2015-08-19 09:20:01.000
gosi75Business Intelligence ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikas GargBusiness Intelligence DeveloperCommented:
HI,

You can try this

SELECT *,ROW_NUMBER()OVER(PARTITION BY CUSTOMERKEY, ACCOUNTNUMBER,QUANTITY ORDER BY SALESORDERID) RN FROM TABLE

Open in new window


The above query will tell you about the records having different salesorderid but common customerkey,accountnumber and quantity.
such records will have RN column having value > 1.

Hope this would help you ...
0
Shaun KlineLead Software EngineerCommented:
Give this a try:
SELECT A.SalesOrderID, B.SalesOrderID
FROM (
   SELECT SalesOrderID, CustomerKey, AccountNumber, Quantity, CreatedDateTime
   FROM Sales) A
   INNER JOIN (
   SELECT SalesOrderID, CustomerKey, AccountNumber, Quantity, CreatedDateTime
   FROM Sales) B ON A.CustoemrKey = B.CustomerKey 
       AND A.AccountNumber = B.AccountNumber
       AND A.Quantity = B.Quantity
WHERE DATEDIFF(mi, A.CreatedDateTime, B.CreatedDateTime) <= 5
      AND A.SalesOrderID <> B.SalesOrderID

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
Using LEAD() and LAG() allows us to compare the CreatedDateTime across rows so that we an do this in a single pass of the table for best performance.
select
    d.SalesOrderID, d.Customerkey, d.AccountNumber, d.Quantity, d.CreatedDateTime
from (
      select
            SalesOrderID, Customerkey, AccountNumber, Quantity, CreatedDateTime
          , datediff(second
                     , lag(CreatedDateTime) over(partition by Customerkey, AccountNumber, Quantity
                                                 order by CreatedDateTime ASC)
                     , CreatedDateTime
                   ) secs1
          , datediff(second
                     , CreatedDateTime
                     , lead(CreatedDateTime) over(partition by Customerkey, AccountNumber, Quantity
                                               order by CreatedDateTime ASC)
                   ) secs2                   
      from Salestable
) d
where ( d.secs1 < (5*60) or d.secs2 < (5*60) )
order by
    d.SalesOrderID, d.Customerkey, d.AccountNumber, d.Quantity, d.CreatedDateTime

Open in new window

Note I have used seconds for precision in the DATEDIFF() calculations so that a difference of 5 minutes and 1 second is excluded from results. If minute was the unit of precision then a difference of 5 minutes 59 seconds would be included in the results.

details:
**MS SQL Server 2014 Schema Setup**:

    CREATE TABLE Salestable 
        ([SalesOrderID] int, [Customerkey] int, [AccountNumber] int, [Quantity] int, [CreatedDateTime] datetime)
    ;
        
    INSERT INTO Salestable 
        ([SalesOrderID], [Customerkey], [AccountNumber], [Quantity], [CreatedDateTime])
    VALUES
        (5001, 250, 1100, 1350, '2015-08-19 09:16:31.000'),
        (5002, 250, 1100, 1350, '2015-08-19 09:20:01.000'),
        (5003, 250, 1100, 1350, '2015-08-22 13:14:15.777')
    ;
    
**Query 1**:

    select
        d.SalesOrderID, d.Customerkey, d.AccountNumber, d.Quantity, d.CreatedDateTime
    from (
          select
                SalesOrderID, Customerkey, AccountNumber, Quantity, CreatedDateTime
              , datediff(second
                         , lag(CreatedDateTime) over(partition by Customerkey, AccountNumber, Quantity
                                                     order by CreatedDateTime ASC)
                         , CreatedDateTime
                       ) secs1
              , datediff(second
                         , CreatedDateTime
                         , lead(CreatedDateTime) over(partition by Customerkey, AccountNumber, Quantity
                                                   order by CreatedDateTime ASC)
                       ) secs2                   
          from Salestable
    ) d
    where ( d.secs1 < (5*60) or d.secs2 < (5*60) )
    order by
        d.SalesOrderID, d.Customerkey, d.AccountNumber, d.Quantity, d.CreatedDateTime
    

**[Results][2]**:
    | SalesOrderID | Customerkey | AccountNumber | Quantity |          CreatedDateTime |
    |--------------|-------------|---------------|----------|--------------------------|
    |         5001 |         250 |          1100 |     1350 | August, 19 2015 09:16:31 |
    |         5002 |         250 |          1100 |     1350 | August, 19 2015 09:20:01 |

  [1]: http://sqlfiddle.com/#!6/a5a05/2

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
gosi75, do you still need help with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.