473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How To Do An Average Of A Count

Hi All

We have an order processing database which includes the standard Order
Header/Order Lines tables. I'm trying to write a query to get the average
number of lines per order over a given period, and I'm stuck :-(

I can get the number of lines per order with:

SELECT COUNT(*) FROM OrderDetails
INNER JOIN Order Header ON
OrderHeader.OrderNo = OrderDetails.OrderNo
WHERE OrderHeader.OrderDate ..... {various criteria} ...
GROUP BY OrderDetails.OrderNumber

But how do I then get an average of this for the period?

TIA
Mike Bannon
Jul 20 '05 #1
5 44314
On Fri, 5 Mar 2004 10:43:46 +0000 (UTC), "Mike Bannon"
<mi***@dataformation.co.uk> wrote:
Hi All

We have an order processing database which includes the standard Order
Header/Order Lines tables. I'm trying to write a query to get the average
number of lines per order over a given period, and I'm stuck :-(

I can get the number of lines per order with:

SELECT COUNT(*) FROM OrderDetails
INNER JOIN Order Header ON
OrderHeader.OrderNo = OrderDetails.OrderNo
WHERE OrderHeader.OrderDate ..... {various criteria} ...
GROUP BY OrderDetails.OrderNumber

But how do I then get an average of this for the period?

TIA
Mike Bannon

Use a derived table. A derived table is a complete SELECT statement in
parentheses and followed by an alias name that is placed in the FROM clause of
another query. In your case, it might be something like this...

SELECT OrderStats.OrderYear, OrderStats.OrderQtr, AVG(OrderStats.OrderLines)
FROM (
SELECT
OrderDetails.OrderYear,
OrderDetails.OrderQtr,
OrderLines = COUNT(*)
FROM OrderDetails
INNER JOIN Order Header ON
OrderHeader.OrderNo = OrderDetails.OrderNo
WHERE OrderHeader.OrderDate ..... {various criteria} ...
GROUP BY
OrderDetails.OrderYear,
OrderDetails.OrderQtr,
OrderDetails.OrderNumber
) OrderStats
GROUP BY
OrderStats.OrderYear,
OrderStats.OrderQtr,

Jul 20 '05 #2

"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:t9********************************@4ax.com...
On Fri, 5 Mar 2004 10:43:46 +0000 (UTC), "Mike Bannon"
<mi***@dataformation.co.uk> wrote:
Hi All

We have an order processing database which includes the standard Order
Header/Order Lines tables. I'm trying to write a query to get the average
number of lines per order over a given period, and I'm stuck :-(

I can get the number of lines per order with:

SELECT COUNT(*) FROM OrderDetails
INNER JOIN Order Header ON
OrderHeader.OrderNo = OrderDetails.OrderNo
WHERE OrderHeader.OrderDate ..... {various criteria} ...
GROUP BY OrderDetails.OrderNumber

But how do I then get an average of this for the period?

TIA
Mike Bannon
Use a derived table. A derived table is a complete SELECT statement in
parentheses and followed by an alias name that is placed in the FROM

clause of another query. In your case, it might be something like this...

SELECT OrderStats.OrderYear, OrderStats.OrderQtr, AVG(OrderStats.OrderLines) FROM (
SELECT
OrderDetails.OrderYear,
OrderDetails.OrderQtr,
OrderLines = COUNT(*)
FROM OrderDetails
INNER JOIN Order Header ON
OrderHeader.OrderNo = OrderDetails.OrderNo
WHERE OrderHeader.OrderDate ..... {various criteria} ...
GROUP BY
OrderDetails.OrderYear,
OrderDetails.OrderQtr,
OrderDetails.OrderNumber
) OrderStats
GROUP BY
OrderStats.OrderYear,
OrderStats.OrderQtr,


Thanks, Steve, works a treat :-)
Mike
Jul 20 '05 #3
>> We have an order processing database which includes the standard
Order
Header/Order Lines tables. <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. My guess is that the order details looks like this:

CREATE TABLE OrderDetails
(order_nbr INTEGER NOT NULL
REFERENCES Orders(order_nbr)
ON UPDATE CASCADE
ON DELETE CASCADE,
upc CHAR(10) NOT NULL -- or other industry code
REFERENCES Inventory(upc)
ON UPDATE CASCADE
ON DELETE CASCADE,
qty INTEGER NOT NULL,
...);
I'm trying to write a query to get the average number of lines per

order over a given period, and I'm stuck :-( <<

I hope that you mean the average number of **items** per order over a
given period. The way you said this would imply that you are copying
the input form (where lines are PHYSICAL), and have no proper LOGICAL
model. If I order my eggs one at a time, I get 12 lines; If I map the
order into a LOGICAL row, I get one row with a quantity of 12 eggs
(and then I can look for packaging units, discounts, etc.)

SELECT AVG(X.tally) AS avg_per_order
FROM (SELECT COUNT(*)
FROM OrderDetails AS D1
WHERE D1.order_nbr
IN (SELECT O1.order_nbr
FROM Orders AS O1
WHERE O1.order_dt
BETWEEN @my_start_dateAND @my_end_date)
GROUP BY order_nbr) AS X(tally);

The trick for aggregates inside aggregates is to use a derived table
for the lower level aggregations, then nest them outward.
Jul 20 '05 #4
Steve Jorgensen (no****@nospam.nospam) writes:
Use a derived table. A derived table is a complete SELECT statement in
parentheses and followed by an alias name that is placed in the FROM
clause of another query. In your case, it might be something like
this...

SELECT OrderStats.OrderYear, OrderStats.OrderQtr,
AVG(OrderStats.OrderLines)


In case you want an average with a fraction, permit me to suggest an
improvement:

SELECT OrderStats.OrderYear, OrderStats.OrderQtr,
AVG(1.0 * OrderStats.OrderLines)

Without 1.0 you will get an integer result.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5
On Sun, 7 Mar 2004 22:26:56 +0000 (UTC), Erland Sommarskog <so****@algonet.se>
wrote:
Steve Jorgensen (no****@nospam.nospam) writes:
Use a derived table. A derived table is a complete SELECT statement in
parentheses and followed by an alias name that is placed in the FROM
clause of another query. In your case, it might be something like
this...

SELECT OrderStats.OrderYear, OrderStats.OrderQtr,
AVG(OrderStats.OrderLines)


In case you want an average with a fraction, permit me to suggest an
improvement:

SELECT OrderStats.OrderYear, OrderStats.OrderQtr,
AVG(1.0 * OrderStats.OrderLines)

Without 1.0 you will get an integer result.


Thanks for correcting my oversight.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: dan | last post by:
this is a program to count average letters per word. i am able to count the total number of letters, but not words. How do you count the total number of words in a text file, so i am able to divide...
5
by: Stephen Miller | last post by:
Hi, I am trying to add a staggered running total and average to a query returning quarterly CPI data. I need to add 4 quarterly data points together to calculate a moving 12-month sum (YrCPI),...
4
by: Gary | last post by:
Hi, I have a temperature conversion program down pat, but I was told to add an average, meaning, i need to get the average temperature for as many times as it was entered. i do not know where to...
3
by: mochatrpl | last post by:
I am looking for a way to make a query / report display the running average for total dollars. I have already set up a query to provide totals dollars per day from which a report graphly shows...
4
by: gaga | last post by:
hi guys, a part of my program requires me to calculate an average of items that are sold. the easiest way to do that would be writing a function, but im having trouble making up the parameters. if...
13
by: vaneric | last post by:
hi i have a set of RGB images of diff faces (of people )as a 2 dim numpyarray ...something like threefaces=array(, , ]) where xa1,xa2,xa3 are tuples each representing rgb values of a pixel...
6
by: kumarboston | last post by:
Hi All, I am trying to get an average value for my data, here is my data file DATA FILE EP1934.PDB 250 250 11.27 EP1934.PDB 251 251 12.7332 EP1934.PDB 252 252 6.38341 EP1934.PDB 253 253...
2
by: jac130 | last post by:
I have an array, populated with student grades. there is a list of student names, but their grades are stored in a class level array. there is a calculate average button,that is supposed to calculate...
5
by: kumarboston | last post by:
Hi all, I was trying to calculate the average value from different parts of the same data file. For example, if suppose we have number 1 - 10 and i was trying to calculate the average of only first...
1
by: Ormazd | last post by:
Hello, I was wondering if anyone might be able to help me with a little PERL script? I'm very new and I have been given a task to write a simple Perl script that prints out the file names and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.