- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
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)
Advertisements