Solved

if then query

Posted on 2011-09-06
5
345 Views
Last Modified: 2012-05-12
Hi, I have a problem with an IF - THEN query:
I have attached the sql commands and the tables and what is my problem.
Sorry that i don't even try articulate the problem in text.



 SQL.doc
0
Comment
Question by:arielbf
  • 4
5 Comments
 
LVL 4

Expert Comment

by:Ara-
Comment Utility
approved
Approved

Capital A in column name, but no in query?
0
 

Author Comment

by:arielbf
Comment Utility
should be approved and not Approved
but this is not the real problem
it is not working anyway
0
 
LVL 4

Expert Comment

by:Ara-
Comment Utility
Sorry, my bad. I can see now that it isn't the problem..
0
 
LVL 4

Expert Comment

by:Ara-
Comment Utility
If you put a sum() around each of the if-entries like this:
sum(if(gp=2,approved,0)) AS aprtra

it should work.
0
 
LVL 4

Accepted Solution

by:
Ara- earned 500 total points
Comment Utility
SELECT sid,  sum(if(gp=1,approved,0)) AS aprstu, sum(if(gp=1,payed,0)) AS paystu, sum(if(gp=2,approved,0)) AS aprtra,sum(if(gp=2,payed,0)) AS paytra, sum(if(gp=3,approved,0)) AS aprenr,sum(if(gp=3,payed,0)) AS payenr, sum(if(gp=4,approved,0)) AS aprdor,sum(if(gp=4,payed,0)) AS paydor FROM grants WHERE sid=1 GROUP BY sid;
+------+--------+--------+--------+--------+--------+--------+--------+--------+
| sid  | aprstu | paystu | aprtra | paytra | aprenr | payenr | aprdor | paydor |
+------+--------+--------+--------+--------+--------+--------+--------+--------+
|    1 |  10000 |   3750 |   5100 |   3000 |   1000 |      0 |      0 |      0 |
+------+--------+--------+--------+--------+--------+--------+--------+--------+
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now