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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:06:49+00:00 2026-06-14T15:06:49+00:00

I have a table that is setup like this: +—-+——-+——-+ | id | col1

  • 0

I have a table that is setup like this:

  +----+-------+-------+
  | id | col1  | col2  |
  +----+-------+-------+
  |  1 | John  | Mike  |
  |  2 | Mike  | John  |
  |  3 | Marty | John  |
  |  4 | Walt  | Marty |
  |  5 | Walt  | Mike  |
  +----+-------+-------+

I basically want to count the unique values in both col1 and col2 and display them alongside the appropriate unique value. The problem is that col1 doesn’t necessarily contain all the same names that col2 has, and vice versa. I’m looking to get it setup like this:

 +-------+-------+------+
 | names | col1  | col1 |
 +-------+-------+------+
 | John  |     1 |    2 |
 | Marty |     1 |    1 |
 | Mike  |     1 |    2 |
 | Walt  |     2 | NULL |
 +-------+-------+------+

I can select these values independently using:

  SELECT col1, count(col1) as count FROM example GROUP BY col1; 

OR

  SELECT col2, count(col2) as count FROM example GROUP BY col2;

But I’m having one hard time understand how I join those two counts together, especially because the value here “Walt” doesn’t appear in col2.

  • 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-14T15:06:50+00:00Added an answer on June 14, 2026 at 3:06 pm

    I’m assuming you could have more cases than you show in the data. You could have NULLs in col1, you could have names that occur only in col1 or only in col2, etc.

    SELECT a.name, c1.`count`, c2.`count`
    FROM (SELECT col1 AS name FROM `Table` UNION SELECT col2 FROM `Table`) a
    LEFT JOIN (SELECT col1, COUNT(*) AS `count` FROM `Table` GROUP BY col1) c1 
      ON a.name = c1.col1
    LEFT JOIN (SELECT col2, COUNT(*) AS `count` FROM `Table` GROUP BY col2) c2 
      ON a.name = c2.col2;
    

    Explanation:
    The derived table a is a union of all names that appear in either column.
    Then make two more derived tables, one with each each name from col1 and the count of how many times it occurs, and then another similar derived tale for names in col2.

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

Sidebar

Related Questions

I have a bill of materials table that is set up like this: item
I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a table named sessions setup like this: ################################# # set # timestamp
I have a table that looks something like this: CREATE TABLE A ( A_UNIQUE_ID
I have a table that is setup like so: ID int Hash varchar OtherID
I have an existing table structure that looks something like this: AnimalTable ------------- |Id
I want to have a setup like this: <div id=block> <div class=btn>2</div> <div class=btn>1235e</div>
I have created a GUI that will setup the pivot table. For example, I
I have setup a simple Oracle external table test that I (alongside a DBA

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.