Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help finding the value of the average order

Author  Topic 

deco10
Starting Member

28 Posts

Posted - 2010-04-23 : 14:47:38
My tables look like this:

Order:
OrderId*
customer
datetime

OrderRows:
OrderId*
RowId
item
cost

How do I get the value of the average order with sql? (I'm using MySQL but I can figure it out if I have the basic syntax).

Thanks :)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 14:50:09
Seems like

SELECT SUM(COst)*1.0/COUNT(DISTINCT OrderId) FROM OrderRows


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-04-23 : 14:54:21
If you mean average per each orderid

SELECT (SUM(COST)+0.0)/COUNT(*) AS AVG
FROM OrderRows
GROUP BY OrderId
Go to Top of Page

deco10
Starting Member

28 Posts

Posted - 2010-04-23 : 15:21:14
Sorry, I left out an important detail of my tables

* = PK
Order:
Customer*
OrderId*
customer
datetime

OrderRows:
Customer*
OrderId*
RowId*
item
cost
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 15:23:36
you need to tell on what basis you want average
just average order value or
average value per order or
average order value per customer

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

deco10
Starting Member

28 Posts

Posted - 2010-04-23 : 15:33:39
Wow all of those would be good now that you mention it.

But for now just the average value of each order.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 15:37:02
then second suggestion will be enough

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -