

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display two different sums of the same price from column Amount in MySQL?
For this, you can use case statement. Let us first create a table −
mysql> create table DemoTable1794 ( Amount int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1794 values(100); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1794 values(80); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1794 values(320); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1794;
This will produce the following output −
+--------+ | Amount | +--------+ | 100 | | 80 | | 320 | +--------+ 3 rows in set (0.00 sec)
Following is the query to display two different sums of the same price from column Amount −
mysql> select sum(Amount), sum( case when Amount < 150 then Amount else 0 end ) as SecondAmount from DemoTable1794;
This will produce the following output −
+-------------+--------------+ | sum(Amount) | SecondAmount | +-------------+--------------+ | 500 | 180 | +-------------+--------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- Concatenate two values from the same column with different conditions in MySQL
- How to select different values from same column and display them in different columns with MySQL?
- How to subtract the same amount from all values in a column with MySQL?
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- Copy from one column to another (different tables same database) in MySQL?
- Display highest amount from corresponding duplicate ids in MySQL
- How come two children from the same family have different nature?
- Display two different columns from two different tables with ORDER BY?
- How to display the count from distinct records in the same row with MySQL?
- How to select and display a list of values in one column that are available in two different MySQL columns?
- Finding the sum of integers from multiple MySQL rows in same column?
- Display the maximum amount of memory in Java
- Display records from the current date till rest of the same month in MySQL?
- MySQL query to calculate the total amount from column values with Cost and Quantity?
- How to show two different colored colormaps in the same imshow Matplotlib?
Advertisements