How to display two different sums of the same price from column Amount in MySQL?

MySQLMySQLi Database

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)
raja
Updated on 23-Dec-2019 12:01:23

Advertisements