Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joined/Mixed query 1

Status
Not open for further replies.

catch0

Programmer
Nov 12, 2007
3
GB
Hi,
I am trying to create the following query in access (using SQL or otherwise).
I have a table which contains (omitting irrelevant ones) 3 fields, say A, B and C. A is something I want to group by, B is a character which is either "X" or "Y" and C is a value I want to display the sum of for each mode in A,
e.g.
SELECT A, Sum(C) FROM table GROUP BY A ORDER BY A;
giving me two columns in the resulting query.
What I really would like would be 3 columns
A,CX,CY where CX is the Sum of all C's within A where B="X" and CY is the equivalent where B="Y" (i.e. for each resulting row C=CX+CY).
How can I tell SQL/Access to perform this split, ideally within an SQL statement since I have similar issues in other queries and dont want to end up creating numerous other queries/tables.
Many thanks in advance
 
SELECT A, Sum(iif(b=x,C,0))cx, Sum(iif(b=y,C,0))FROM table GROUP BY A ORDER BY A;
 
sorry for the typo

Code:
SELECT A, Sum(iif(b=x,C,0))cx, Sum(iif(b=y,C,0))cy FROM table GROUP BY A ORDER BY A;
 
brilliant. seems to be working, many thanks for the speedy reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top