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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:50:03+00:00 2026-06-17T14:50:03+00:00

Suppose I have a table: col1, col2, col3 1 0 1.3 0 0 0

  • 0

Suppose I have a table:

col1, col2, col3
1     0     1.3
0     0     0
0     1     0
1     1.5   1

Now, let’s say each row has a “weight” calculated as this:

(col1 > 0 ? 1 : 0) + (col2 > 0 ? 1 : 0) + (col3 > 0 ? 1 : 0)

How can I select the total weight of all rows?

With the data I have given the total weight is 2+0+1+3=6

  • 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-17T14:50:04+00:00Added an answer on June 17, 2026 at 2:50 pm

    You just need one aggregate SUM() surrounding all the conditions with no GROUP BY.

    SELECT
      SUM(
       (CASE WHEN col1 > 0 THEN 1 ELSE 0 END)
       + (CASE WHEN col2 > 0 THEN 1 ELSE 0 END)
       + (CASE WHEN col3 > 0 THEN 1 ELSE 0 END)
      ) AS total_weight
    FROM your_table
    

    http://sqlfiddle.com/#!2/b0d82/1

    I am using CASE WHEN... here as it is portable across any RDBMS. Since MySQL will return a boolean 0 or 1 for the conditions though, it can be simplified in MySQL as:

    SELECT
      SUM(
       (col1 > 0) /* returns 1 or 0 */
        + (col2 > 0)
        + (col3 > 0)
       ) AS total_weight
    FROM your_table
    

    http://sqlfiddle.com/#!2/b0d82/2

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

Sidebar

Related Questions

Suppose I have a table like this: id col1 col2 col3 1 a 1
suppose that I have this RDBM table ( Entity-attribute-value_model ): col1: entityID col2: attributeName
Suppose I have a table ABC and table XYZ. Columns of ABC-->col1 col2 col3
Suppose I have a table as follows: (on DB2 9.7.2) COL1 COL2 COL3 -----------
Suppose I have columns col1 , col2 , col3 , col4 in myTable and
Let's suppose we have a table with the fallowing values introduced in this particular
Suppose I have this table parent | child 1 2 1 3 2 4
Suppose I have this table: id | name | city ------------------ 1 | n1
Suppose I have table A and B. Table A has a FK to Table
Suppose I have table first_table which has an FK to table second_table table second_table

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.