Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# SQL Query for AVG

Posted on 2013-01-21
Medium Priority
245 Views
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
[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

Author Comment

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

ID: 38803692
select year(column1), month(column1), avg(column2)
from yourtable
group by year(column1), month(column1)
0

LVL 4

Expert Comment

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]
``````
0

LVL 71

Expert Comment

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"
``````
This way you do not need to repeat the expression in group by.
0

LVL 32

Expert Comment

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

Author Comment

ID: 38810684
downtime is numeric
0

LVL 70

Accepted Solution

Éric Moreau earned 2000 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

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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…
###### Suggested Courses
Course of the Month5 days, 17 hours left to enroll