SQL: How to check a value in column is equal to the sum of any combination of the other columns in a row

Syed Zaidi

I came across a scenario where I have a table structure as such:

Column0 Column1 Column2 Column3 Column4 Column5 
100     U       V       X       Y       Z

I need to find if:

  • 100 = U, V, X, Y, or Z.
  • 100 = X + Y + Z + V
  • 100 = X + Y
  • 100 = V + Z
  • etc.

Any suggestions of how I can achieve this?

Case statements will take forever to write. I do not write PL/SQL code but I am familiar with it somewhat.

Siyual

Writing the Case statement isn't as hard as you're implying. I generated the following Case statement using a text editor in about 20 seconds, which should handle all situations in your example:

Select  Case
            When Column0 = Column1  Then 1
            When Column0 = Column2  Then 1
            When Column0 = Column3  Then 1
            When Column0 = Column4  Then 1
            When Column0 = Column5  Then 1

            When Column0 = Column1 + Column2    Then 1
            When Column0 = Column1 + Column3    Then 1
            When Column0 = Column1 + Column4    Then 1
            When Column0 = Column1 + Column5    Then 1

            When Column0 = Column2 + Column3    Then 1
            When Column0 = Column2 + Column4    Then 1
            When Column0 = Column2 + Column5    Then 1

            When Column0 = Column3 + Column4    Then 1
            When Column0 = Column3 + Column5    Then 1

            When Column0 = Column4 + Column5    Then 1

            When Column0 = Column1 + Column2 + Column3  Then 1
            When Column0 = Column1 + Column2 + Column4  Then 1
            When Column0 = Column1 + Column2 + Column5  Then 1

            When Column0 = Column1 + Column3 + Column4  Then 1
            When Column0 = Column1 + Column3 + Column5  Then 1

            When Column0 = Column1 + Column4 + Column5  Then 1

            When Column0 = Column2 + Column3 + Column4  Then 1
            When Column0 = Column2 + Column3 + Column5  Then 1

            When Column0 = Column2 + Column4 + Column5  Then 1

            When Column0 = Column3 + Column4 + Column5  Then 1

            When Column0 = Column1 + Column2 + Column3 + Column4    Then 1
            When Column0 = Column1 + Column2 + Column3 + Column5    Then 1

            When Column0 = Column1 + Column3 + Column4 + Column5    Then 1

            When Column0 = Column2 + Column3 + Column4 + Column5    Then 1

            When Column0 = Column1 + Column2 + Column3 + Column4 + Column5  Then 1

            Else 0
        End As SumOfOtherColumns
From    YourTable

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sum column if other columns equal value

Check if any combination of columns are equal some specific value in matrix

How to change a value in a column based on whether or not a certain string combination is in other columns in the same row? (Pandas)

How to check one rows value present in any of the other column row value

For each row check if value in one column exists in two other columns

How to check if any value in group is equal to a specific value in BigQuery SQL?

Count how many times any value appears in a combination of columns but not in other columns (pandas)

SQL How to select columns multiple times on one row based on value in other column

How to sum up values of 'D' column for every row with the same combination of values from columns 'A','B' and 'C?

How to SUM() columns based on other column values SQL

check if row value value is equal to column name and access the value of the column

How to create a new dataframe row with a new column for every combination of other two columns?

unpivot columns, sum of value into one row group by other column based on start_month column that is bound to change

Check Unique Column Values Based on Combination of other Column Value

Check if the value exists in any other columns with Tidyverse

Check if one row is equal to any other rows in R

Ignore a row in SQL if one column value is equal and another column is not equal

Python: Assign missing value to rows in one column if any row is missing value in other columns

How to check if a value exists in any of the columns in a table in sql

How to check if any value is Null in a table single row SQL

How to change a column value, based on a combination of values from two other columns in R?

Select column value that matches a combination of other columns values on the same table

How to find the first cell in a row where value is not empty and check if the number is less or equal the number in other cell

How to check other column value

Creating column that is a combination of other columns

How to check if a column (of type array) contains a value in SQL (without ANY)?

SQL Check constraint on column referencing other columns

In SQL, how to select minimum value of a column and group by other columns?

How to sum a column where another column is equal to other table

TOP Ranking

HotTag

Archive