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

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)
Updated on: 2019-12-23T12:01:23+05:30

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements