Solved

SQL Query for AVG

Posted on 2013-01-21
7
230 Views
Last Modified: 2013-02-26
Table A: Column1: DateTime
               Column2: Downtime

I want to display the average downtime over the last month. I want a single value returned.

How do I build this query
0
Comment
Question by:adimit19
[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
7 Comments
 

Author Comment

by:adimit19
ID: 38803687
I forgot to add another criteria. I would actually want 12 values for the year, but the average calculated monthly.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38803692
select year(column1), month(column1), avg(column2)
from yourtable
group by year(column1), month(column1)
0
 
LVL 4

Expert Comment

by:krtyknmsql
ID: 38804089
I assume the column Downtime has only time value and the query below gives you a expected result.

SELECT 
	YEAR([DateTime]) [Year],
	UPPER(LEFT(DATENAME(MM,[DateTime]),3)) [Month],
	CONVERT(VARCHAR, CAST(AVG(CAST(CAST(LEFT(DownTime, 12) AS DATETIME) AS FLOAT)) AS DATETIME), 114) + '0'
FROM Tab
GROUP BY 
	YEAR([DateTime]) [Year],
	UPPER(LEFT(DATENAME(MM,[DateTime]),3)) [Month]

Open in new window

0
Technology Partners: 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!

 
LVL 70

Expert Comment

by:Qlemo
ID: 38804408
If expressions to group for get more complicated, it is preferrable to use this syntax:
select "year", "month", avg(downtime)
from (
 select "year"=year("DateTime"), "month"=month("DateTime"), downtime
 from tableA
) tbl
group by "year", "month"

Open in new window

This way you do not need to repeat the expression in group by.
0
 
LVL 32

Expert Comment

by:awking00
ID: 38806660
What is the datatype of downtime? Some sample values and expected results might help.
0
 

Author Comment

by:adimit19
ID: 38810684
downtime is numeric
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 38810697
have you tried my query:

select year(datetime), month(datetime), avg(downtime)
from yourtable
group by year(datetime), month(datetime)
0

Featured Post

Independent Software Vendors: 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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

739 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