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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

differen aggregations IN ONE QUERY 1

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
CH
Hi,

Hi all

I have a gui interface that allows 1 single query statement to output results. Now, I would need to output:
a) the total for each project
b) the total for each project in each year
in one query.

hmm... can I join recursivley (join itself) to solve the problem?

What if I need the total costs of Project 4 and the yearly costs next to the total? Hardcoding the years would be fine for me.

TABLE
ProjectID, Year, TotalCosts
2, 2004, 500
2, 2005, 300
3, 2004, 100
4, 2004, 200
4, 2005, 300
4, 2006, 800

RESULT I NEED
ProjectID, ProjectTotal, Year2004, Year2005, Year2006
4, 1300, 200, 300, 800

thanks for any hint
 

Use CASE statement:
Code:
Select ProjectID
           , SUM(TotalCosts) ProjectTotal
           , SUM(CASE WHEN Year=2004 Then TotalCosts ELSE 0 END) Year2004
           , SUM(CASE WHEN Year=2005 Then TotalCosts ELSE 0 END) Year2005
           , SUM(CASE WHEN Year=2006 Then TotalCosts ELSE 0 END) Year2006
  From MyTable
 Group By ProjectID;
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
I had to use IIF(condition, output1, output2) instead of CASE (using MS Access 2000). However, thanks for leading me to the solution. That's worth a start, no doubt.
 
using MS Access 2000
Why not simply follow the Crosstab query wizard ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top