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

  • Home
  • SEARCH
  • 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 6327135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:13:29+00:00 2026-05-24T17:13:29+00:00

I need to find customers who have made identical orders. (Using T-SQL) Order OrderID

  • 0

I need to find customers who have made identical orders. (Using T-SQL)

Order

OrderID Customerer  
1   2   
2   5   
3   6   
4   2   
5   4   
6   6   
7   8

OrderLine

OrderLineID OrderID OrderDate   OrderType   Quantity    Reference  
1   1   01/01/2011  1   1   Coca Cola  
2   1   01/01/2011  1   3   Tea  
3   2   02/02/2011  2   1   Coffee  
4   2   02/02/2011  2   2   Solo  
5   2   03/02/2011  1   1   Soda  
6   3   03/02/2011  1   3   Tea  
7   3   03/02/2011  1   1   Coca Cola  
8   4   05/06/2011  1   1   Beer  
9   5   06/06/2011  2   1   Tea  
10  5   06/06/2011  2   1   Coca Cola  
11  6   07/07/2011  1   1   Coffee  
12  6   07/07/2011  1   2   Solo  
13  6   07/07/2011  1   1   Soda  
14  6   07/07/2011  1   1   Beer  
15  7   08/08/2011  1   1   Beer  

Here orders with OrderID 1 and 3 are considered to be identical because the number for orderlines, “Quantity” and “Reference” are identical on both orders. Meaning that customer 2 and 6 have placed identical orders.

Order 5 are not identical to order 1 and 3 because Quantity differ.

Order 2 are not identical to order 6 because orderlines differ.

Order 4 and 7 are also identical.

I am searching for a ressult like this:

IdenticalOrders

OrderID CustomeerID  
1   2  
3   6  
4   2  
7   8  

It seems like an easy task, but I just can’t understand where to start.
(I am still new to t-sql 🙂 )

  • 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-24T17:13:30+00:00Added an answer on May 24, 2026 at 5:13 pm

    This is an extension of Martin’s second suggestion. This will show all matching combinations without any repetitions.

    ;With FmtOL(customer, orderid, complete_order) as
    (            
    SELECT  customer, orderid, complete_order
         FROM    Order O
                        cross apply ( SELECT    CAST(Quantity AS VARCHAR(30))
                                                + '~' + Reference + '~~'
                                      FROM      OrderLine OL
                                      WHERE     OL.OrderID = O.OrderID
                                      ORDER BY  Reference ,
                                                Quantity
                                    FOR
                                      XML PATH('')
                                    ) T ( complete_order )
    )
    SELECT  T1.OrderId, 
            T1.Customer,
            STUFF(C1.a, 1, 2, '') as [SameAs]
    FROM    FmtOL T1
    Cross apply ( SELECT '; ' + 'Customer ' + Cast(T2.Customer as varchar(30)) 
                      + '''s order ' + Cast(T2.OrderID as varchar(30))
                     FROM   FmtOL T2
                     WHERE  T1.Customer < T2.Customer
                       AND T1.OrderId < T2.OrderId
                       AND T1.complete_order = T2.complete_order
                     order by ';' + Cast(T2.Customer as varchar(30)) 
                        + '''s order ' + Cast(T2.OrderID as varchar(30))
                        , t2.orderid
                     for xml path('')
                 ) C1 (a)
      where C1.a is not null
    

    Results should look like this:

    OrderId Customer SameAs 
    1       2        Customer 6's order 3 
    4       2        Customer 8's order 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have customers who, for unassailable reasons, cannot use SQL Server's built-in backup features
Question resolved I have two tables, orders and customers, and I'd like to find
I need to find occurrences of (+) in my sql scripts, (i.e., Oracle outer
INFORMIX-SQL 7.32 (SE) Linux Pawnshop App. I have some users who own several pawnshops
I need to find out rank of customers. Here I am adding the corresponding
We are using Microsoft reporting services in order to generate reports for our customers.
I have to estimate the time we need to upgrade one of our customers
I'm writing a query in SQL in which i have to find the youngest
I need to find out how to format numbers as strings. My code is
I need to find a bottleneck and need to accurately as possible measure time.

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.