- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display the sum of positive and negative values from a column in separate columns with MySQL
For this, you can use CASE statement. Let us first create a table −
mysql> create table DemoTable( Id int, Value int ); Query OK, 0 rows affected (0.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,100); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(10,-110); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(10,200); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(10,-678); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+-------+ | Id | Value | +------+-------+ | 10 | 100 | | 10 | -110 | | 10 | 200 | | 10 | -678 | +------+-------+ 4 rows in set (0.00 sec)
Following is the query to display the sum of positive and negative values from a column in separate columns −
mysql> select Id, sum(case when Value>0 then Value else 0 end) as Positive_Value, sum(case when Value<0 then Value else 0 end) as Negative_Value from DemoTable group by Id;
This will produce the following output −
+------+----------------+----------------+ | Id | Positive_Value | Negative_Value | +------+----------------+----------------+ | 10 | 300 | -788 | +------+----------------+----------------+ 1 row in set (0.00 sec)
- Related Articles
- Concatenate the column values with separate text in MySQL and display in a single column
- Select distinct values from three columns and display in a single column with MySQL
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- MySQL query to separate and select string values (with hyphen) from one column to different columns
- How to select different values from same column and display them in different columns with MySQL?
- Display MySQL histogram with negative values?
- Multiply values of two columns and display it a new column in MySQL?
- Count duplicate ids and display the result in a separate column with MySQL
- Fetch the size of a specific column values in MySQL and display the sum
- Divide numbers from two columns and display result in a new column with MySQL
- Count values greater and less than a specific number and display count in separate MySQL columns?
- Concatenate date and time from separate columns into a single column in MySQL
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- MySQL query to count the duplicate ID values and display the result in a separate column
- Select multiple sums with MySQL query and display them in separate columns?

Advertisements