Solved

SQL Query for AVG

Posted on 2013-01-21
7
191 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 69

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 68

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 31

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 69

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now