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!

Aggregate data in one SELECT statement 1

Status
Not open for further replies.

hallux

Programmer
Feb 25, 2003
133
US
Using Oracle 8i:
I need to summarize total sales by employee. Each invoice can have multiple items sold.

sales1 table contents:
employee, invoice, qty, price
smith, 5055, 2, 200
smith, 5055, 2, 50
smith, 5056, 1, 310
smith, 5058, 1, 438
jones, 5047, 1, 400
jones, 5047, 2, 125
richie, 5098, 1, 435
richie, 5099, 1, 230

SELECT employee, invoice, sum(qty * price) as ext_price FROM sales1;
employee, invoice, ext_price
smith 5055 500
smith 5056 310
smith 5058 438
jones 5047 650
richie 5098 435
richie 5099 230

I would like to then have it summarize the total sales per employee and then divide that by the invoice count for the average sales per employee. So, this would be the expected final output.

employee count sum ave
smith 3 1248 416
jones 1 650 650
richie 2 665 333

How can I make this happen in one SELECT statement? Is that possible?

-Hallux
 
Code:
select employee,count(distinct invoice),
  sum(qty*price), sum(qty*price)/count(distinct invoice)
  from sales1
group by employee
 
Thank you very much. It worked perfectly.
-Hallux
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top