Solved

SQL to multiply values on each row and then find the average

Posted on 2008-06-26
4
1,353 Views
Last Modified: 2010-04-21
Hi there,

I have a table Customer with the following details

AcctID,  Name, Ticket, Values

1            Bob       3           20
2            Bob       3           10
3           Bob        4            5
4           Bob        5          10
5           Bob        5          10
6           Karen     6           2

What I need to do is write a SQL query to multiply the values together for each ticket and then return the Average Value per user(Name). - The Average Value is the multiplied values for each ticket divided by the number of tickets.

Please can you help me write the SQL query to return the average values per user.

Many thanks.
0
Comment
Question by:ihatelag
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21873274
you mean:
select name, avg(Ticket * Values)
 from customer
group by name

Open in new window

0
 

Author Comment

by:ihatelag
ID: 21874960
I need to multiply the figures in the Value column not Column Ticket and Column Values,

For example  for ticket 3 - bob has 10 * 20 in the 'Values' column  so ticket 3 = 200 from 1 ticket
then we take each ticket he has run the above formulae and then get the avarage value after multiplying the values.

Only if the query was just as simple as you wrote it ;)
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21875090
it's as simple as that, with a "small" math trick:
set nocount on
declare @t table (acctID int ,  Name varchar(30), Ticket int, [Values] int)
insert into @t values(1            ,'Bob',       3         ,  20 )
insert into @t values(2            ,'Bob',       3         ,  10)
insert into @t values(3           ,'Bob',        4         ,   5)
insert into @t values(4           ,'Bob',        5         , 10)
insert into @t values(5           ,'Bob',        5         , 10)
insert into @t values(6           ,'Karen',     6          , 2)
 
-- step 1 to show the products:
select name, ticket, cast( exp(sum(log([values]))) as int) p
from @t
group by name, ticket
 
 
-- step 2 to average by user
select name, avg(p) average_value, count(*) count_tickets, sum(p) sum_product
from (
select name, ticket, cast( exp(sum(log([values]))) as int) p
from @t
group by name, ticket
) l
group by name

Open in new window

0
 

Author Closing Comment

by:ihatelag
ID: 31470912
I wasn't able to apply all of the query as the table didn't just contain the lines of the example, but I used a neat the step 1 and step 2 to good use. Many thanks for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

728 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