Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
4 views
in SQL by (106k points)

SELECT id, amount FROM report

I need amount to be amount if report.type='P' and -amount if report.type='N'. How do I add this to the above query?

1 Answer

+3 votes
by (40.7k points)
edited by

SELECT Id, IF(type = 'P1', amount, amount * -1) as amount

FROM report

If the condition is null, in the case of a null amount you can use this to handle it:

SELECT Id, IF(type = 'P1', IFNULL(amount,0), IFNULL(amount,0) * -1) as amount

FROM report

Are you interested in learning SQL from scratch! Have a look at this interesting video on SQL provided by Intellipaat:

In the Above code, IFNULL(amount,0) this means it will return amount if the amount is assigned not null but if the amount is assigned as null then it’ll result in ‘zero’. 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 10, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...