0

I have a table, called mess_stock. mess_stock table has following values,

sno    pay_amount   pay_type
1       10           1
2       60           1
2       500          2

I want to sum the payment by its payment type and wants to rename it as mess_pay if pay_type is 1, est_pay if pay_type is 2.

I have tried the following query

select pay_amount,sum(if(pay_type=1, pay_amount, 0)) mess_pay, sum(if(pay_type=2, pay_amount, 0)) est_pay from mess_stock group by pay_type;

this will gives as the following value

+------------+----------+---------+
| pay_amount | mess_pay | est_pay |
+------------+----------+---------+
|      10.00 |    70.00 |    0.00 |
|     500.00 |     0.00 |  500.00 |
+------------+----------+---------+

But I need answer like this

+----------+---------+
| mess_pay | est_pay |
+----------+---------+
|    70.00 |  500.00 |
+----------+---------+

What can I do?

1 Answer 1

1

Try this:-

SELECT SUM(IF(pay_type = 1, pay_amount, 0)) mess_pay, SUM(IF(pay_type=2, pay_amount, 0)) est_pay 
FROM mess_stock;

I hope this can help you.

Sign up to request clarification or add additional context in comments.

2 Comments

it said ERROR 1111 (HY000): Invalid use of group function
this works fine SELECT SUM(IF(pay_type = 1, pay_amount, 0)) mess_pay, SUM(IF(pay_type=2, pay_amount, 0)) est_pay FROM mess_stock;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.