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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:43:22+00:00 2026-05-27T16:43:22+00:00

I have been banging my head over this one for quite a while now

  • 0

I have been banging my head over this one for quite a while now but can not seem to get it working 🙁

I have a table which among other standard fields, also has a few fields for different values of a same kind (INT for example). Value of each field is unique -> meaning if it appears in a val_1 it will not appear in val_2 and val_n. It can be however repeated in next row. My table looks something like this:

| id | val_1 | val_2 | val_3 | ... | val_n | some other stuff | ... |

My question is: How can I count occurrences of certain values in my table? For example: I would like to know which value occurs the most.

  • 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-27T16:43:22+00:00Added an answer on May 27, 2026 at 4:43 pm
    SELECT
      COUNT(DISTINCT val_1) AS val_1_count,
      COUNT(DISTINCT val_2) AS val_2_count,
      ...
    FROM ...
    

    will give you the counts for each field.

    SELECT val_1, count(*) as val_1_count
    FROM ...
    GROUP BY val_1
    

    will give you the counts for a value. You can use UNION to repeat this for val_1 to val_n in a single (kludgy) query.

    If you want the counts over all fields, you need

    SELECT val,count(*) as valcount
    FROM (
      SELECT val_1 AS val FROM ...
      UNION ALL
      SELECT val_2 AS val FROM ...
      ...
    ) AS baseview
    GROUP BY val
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been banging my head for quite a while with this and can't
I have been banging my head against this one for a while now and
Been banging my head on this for a while now The problem I have
I've been banging my head on this one for a while now. I want
Ok, I have been banging my head on this one for a while, figure
I have been banging my head against this for a little while now. I
I have been banging my head on this one all day. The C++ project
I have been banging my head on a wall with this one. I need
I've been banging my head on this one for a while and I figured
I've been banging my head on this one for a while. Someone please resque

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.