Solved

if then query

Posted on 2011-09-06
5
351 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-
ID: 36488351
approved
Approved

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

Author Comment

by:arielbf
ID: 36488367
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-
ID: 36488388
Sorry, my bad. I can see now that it isn't the problem..
0
 
LVL 4

Expert Comment

by:Ara-
ID: 36488407
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
ID: 36488420
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…

740 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