Tuesday, May 29, 2012

MySql Query: PLSQL Conditions within query

Suppose we have a table like:
transaction_id acc_no amount
1 100001 20000
2 100002 30000
3 100003 30000
4 100004 10000
5 100005 50000
Now the query
select
    acc_no,
    SUM(IF(amount>20000,amount,0)) as total_value
from
    account
where
    acc_no=100002
group by acc_no;

will produce the following result:
 
acc_no amount
100002 80000
Case statements can also be used within a query like shown in the answer of this stackoverflow question

No comments:

Post a Comment