
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to select the sum of the column values with higher value in reach row with MySQL?
Use the CASE statements and set conditions for the same. Let us first create a table −
mysql> create table DemoTable -> ( -> X int, -> Y int -> ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(20,30); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(40,15); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(80,85); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+------+------+ | X | Y | +------+------+ | 20 | 30 | | 40 | 15 | | 80 | 85 | +------+------+ 3 rows in set (0.00 sec)
Following is the query to select the sum of the column values with higher value in each row.
mysql> SELECT SUM(CASE WHEN X > Y THEN X ELSE Y END) from DemoTable;
Output
This will produce the following output −
+----------------------------------------+ | SUM(CASE WHEN X > Y THEN X ELSE Y END) | +----------------------------------------+ | 155 | +----------------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- MySQL SELECT to sum a column value with previous value
- Add a character in the end to column values with MySQL SELECT?
- How to do a sum of previous row value with the current row and display the result in another row with MySQL cross join?
- How to select row when column must satisfy multiple value in MySQL?
- How can I select the row with the highest ID in MySQL?
- How to select the maximum value of a column in MySQL?
- Set the NULL values to 0 and display the entire column in a new column with MySQL SELECT
- How to add a column from a select query but the value from the new column will be the row count of the MySQL select query?
- MySQL query to select column values ending with certain character/number?
- MySQL query to find sum of fields with same column value?
- MySQL query to select the nth highest value in a column by skipping values
- Pad the values of the column with zeros in MySQL
- How to sum the values in the table by month with MySQL?
- How to select a column name with spaces in MySQL?

Advertisements