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 7964063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:47:03+00:00 2026-06-04T05:47:03+00:00

I wounder if its possible to find out using sql the most frequent pairs

  • 0

I wounder if its possible to find out using sql the most frequent pairs of numbers which appear in the table. ie:
I’ve got few columns populated with numbers:

num1, num2, num3, num4, num5, num6
2      3      4     5     6    7

Of course the number of rows is larger but you got an idea..
The goal is to display for example 10 pairs of numbers which appeared in rows.

Please let me know if its possible, or I should rather perform a “brute-force” kind of solution.

Regards

  • 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-06-04T05:47:05+00:00Added an answer on June 4, 2026 at 5:47 am

    In order to solve this problem, you need to do the following:

    1. Unpivot the columns
    2. Perform a self-join to get pairs
    3. Summarize to get the most common pairs.

    Let me give a solution that will work in SQL Server, and with very minor modifications in mysql:

    with allnums as (
            select id, 1 as col, num1 as num from t union all
            select id, 2 as col, num2 as num from t union all
            select id, 3 as col, num3 as num from t union all
            select id, 4 as col, num4 as num from t union all
            ...
        )
    select top x an1.num, an2.num, count(*)
    from allnums an1 join
         allnums an2
         on an1.id = an2.id and
            an1.col < an2.col
    order by count(*) desc
    

    I don’t think mysql supports the “with” syntax, so you need to either repeat the subquery twice, put the results in a temporary table, or create a view.

    You can also express the same idea with database specific constructs, such as “unpivot” in SQL Server. This can somewhat simplify the SQL, but not by much.

    If you don’t have a unique id for each row, then an easy thing to do in SQL Server is to use the row_number() function to assign one, in another “with” statement. Alternatively, you could possibly just convert all the numbers to character strings and then concatenate them together.

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

Sidebar

Related Questions

I'm a new Java developer and would like to find out if it's possible
I wounder how its possible to make a shape like the example, with round
I wonder If and how it is possible to find out if say Calc.exe
I wonder if it's possible to create an extension method which has functionality &
I wonder if it's possible to show a warning or open a pop-up, which
I wonder if its possible to keep child elements from being animated. like in
I am using emacs + slime for clojure development. Recently we got a new
I wonder if its possible to have a watermark on hotlinked images on an
There are two methods, one of which returns a data using LINQ within a
I wonder is it possible to run my application before login on windows.? Its

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.