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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:40:45+00:00 2026-05-11T20:40:45+00:00

I have a table like this: col1 col2 2001 3001 2002 3001 2003 3001

  • 0

I have a table like this:

col1   col2

2001   3001
2002   3001
2003   3001
2004   3002
2002   3003
2001   3003

i want to create it a query that finds out all the combination of items in col1 with a certain item in col1 if they occur to the same item in col2 and the frequency of occurrence. For example, if we want to explore item number “2001” in col1 the query should return:

col3   col4   col5
2001   2002   0.667
2001   2003   0.333

Can someone give me a hint? Thanks ahead.

  • 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-11T20:40:45+00:00Added an answer on May 11, 2026 at 8:40 pm

    Thinking of it this way, you are trying to relate the table to itself through col2, so we’ll start off with a join onto itself:

    select left.col1, right.col1/*, ...*/ from table left inner join table right on left.col2 = right.col2 where left.col1 = '2001' /* ... */
    

    Now for your aggregate. You want to aggregate all right.col1 to get the count for that column:

    select left.col1, right.col1, COUNT(*) from table left inner join table right on left.col2 = right.col2 where left.col1 = '2001' group by right.col2
    

    Which I believe should come out with the raw counts. I believe you’ll have to spin over the query and get a total to get the frequency.

    As a note, this would be a lot more complicated if col1 weren’t constant.

    Edited to add: If you are looking for one query, instead of iterating over it in whatever language you are querying from, you would need to do this grouping twice:

    SELECT abs_freq.col1, abs_freq.col2, abs_freq.freq / totals.total
    FROM (SELECT
           left.col1 AS col1,
           COUNT(*)  AS total
          FROM TABLE LEFT
          INNER JOIN TABLE RIGHT
            ON left.col2 = right.col2
          WHERE left.col1 = '2001'
          GROUP BY left.col1
          ) totals
    INNER JOIN (SELECT
                  left.col1  AS col1,
                  right.col1 AS col2,
                  COUNT(*)   AS freq
                FROM TABLE LEFT
                INNER JOIN TABLE RIGHT
                  ON left.col2 = right.col2
                WHERE left.col1 = '2001'
                GROUP BY right.col2
                ) abs_freq
       ON abs_freq.col1 = totals.col1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)
I have a table like this Table1 Col1 Col2 Col3 Col4 Col5 Col6 Col7
I have a table like this Col1 Col2 Mindate MaxDate A B 12-04-2012 00:12:00:087
I have a table that looks like this: Col1 Col2 Col3 Col4 a b
I have a table that looks like this: id, col1, col2 col3 0 Goat
I have table like this col1 col2 col3 3 5 8 4 5 5
I've got a #TempTble which looks like this: Col1,Col2,5,8,19,.... Also, I have another table
I have a table like this in an excel spreadshhet: Col1 | Col2 |
Lets say i have a table like this: col1 col2 col3 row1 1 null
I have a table that looks like this: col1 col2 col3 ------ ----- -----

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.