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

Need Sum breakout...'transform'-ish SQL

Status
Not open for further replies.

Shrum

Programmer
May 17, 2002
122
US
I have a client that wants to generate a report (no biggie there).

The db contains various fields (in multiple tables) but ultimately the ones that I'm concerned with are:

user id, year, donation

The client wants to be able to group by sum the donations by year by user. Here's an example:

User ID|year|donation
1|1999|1.00
1|1999|2.00
2|1999|1.00
2|2000|2.00

I need to be able to generate a report like:

+1999
+Users that gave $1.00
User 2
+Users that gave $3.00
User 1

+2000
+Users that gave $2.00
User 2

The '+' grouping is on SUM by year and user. Note that user 1 does not list under $1.00 (even though he donated $1.00 but totaled $3.00 in that year)

How can I structure a SQL query so I can generate a report on it?

TIA

Sean Shrum
sean@shrum.net
sean_shrum@hotmail.com
 
Use SQL aggregate functions and Group By. You should be able to use a reporting tool to create a report from the result set of the query.

Select
[Year], [User ID], TotDonation=Sum(Donation)
From Table
Group By [Year], [User ID]; Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top