Solved

SQL Query for AVG

Posted on 2013-01-21
7
220 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
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 69

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

828 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