Solved

SQL Query Help

Posted on 2008-09-30
11
201 Views
Last Modified: 2010-04-21
Hello EXPERTS! What am I doing wrong on this query? I would like to be able to compute the counts column for AVG, MIN, MAX and SUM.

SELECT DATEADD(mi, -15, dtcompleted) as Timeframe, count(1) as counts

from sds (nolock)

where dtcompleted between '2006-06-26' and '2006-06-27'

group by dtcompleted

order by dtcompleted, 1

compute AVG(counts), SUM(counts), MAX(counts), MIN(counts)
0
Comment
Question by:drei22
[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
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 50 total points
ID: 22609273
Compute is used to do stuff like a subtotal when a sum is used above in the query:

Try this:

SELECT dtCompleted,AVG(counts), SUM(counts), MAX(counts), MIN(counts)
from sds (nolock)
where dtcompleted between '2006-06-26' and '2006-06-27'
group by dtcompleted
0
 
LVL 11

Expert Comment

by:aaronakin
ID: 22609304
Is this what you're looking for?

WITH sds_tmp (Timeframe, counts) AS
(
  SELECT DATEADD(mi, -15, dtcompleted), count(1)
    FROM sds (nolock)
    WHERE dtcompleted between '2006-06-26' and '2006-06-27'
    GROUP BY dtcompleted
    ORDER BY dtcompleted
)
SELECT AVG(counts), SUM(counts), MAX(counts), MIN(counts)
  FROM sds_tmp
0
 
LVL 11

Expert Comment

by:aaronakin
ID: 22609311
Sorry, I forgot to remove the ORDER BY clause from my previous post.

WITH sds_tmp (Timeframe, counts) AS
(
  SELECT DATEADD(mi, -15, dtcompleted), count(1)
    FROM sds (nolock)
    WHERE dtcompleted between '2006-06-26' and '2006-06-27'
    GROUP BY dtcompleted
)
SELECT AVG(counts), SUM(counts), MAX(counts), MIN(counts)
  FROM sds_tmp
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 22

Accepted Solution

by:
dportas earned 100 total points
ID: 22609428
You can't use the column alias in the query. Also best to avoid COMPUTE, which is deprecated. Try the following.

Notice I've changed the date range because your version would have excluded any rows with times after midnight on the 27th.

SELECT DATEADD(mi, -15, dtcompleted) as Timeframe,
 SUM(cnt) cnt,
 MIN(cnt) min_cnt,
 MAX(cnt) max_cnt,
 AVG(cnt) avg_cnt
FROM
(SELECT dtcompleted, COUNT(*) as cnt
 FROM sds
 WHERE dtcompleted >= '20060626'
  AND dtcompleted < '20060628'
 GROUP BY dtcompleted
) T
GROUP BY dtcompleted
WITH ROLLUP;
0
 

Author Comment

by:drei22
ID: 22609480
I think I mistated one little detail AARONAKIN I'm using SQL 2000 not 2005. So I can't use the WITH clause.
0
 
LVL 11

Expert Comment

by:aaronakin
ID: 22609525
Ah, sorry I should have asked.  I saw that you posted in the 2005 zone, and I just assumed.  My bad.

In that case, I'd go with dportas's solution.
0
 

Author Comment

by:drei22
ID: 22609590
Again I apologize for that mistake AARONKIN. And DPORTAS everything looks good but when I run the query I get this.

Timeframe                               cnt   min_cnt    max_cnt   avg_cnt
2006-06-20  00:00:00.000        1          1                1              1


I was looking to see total total numbers for the counts. So total counts for each cnt column for the stated time period

Any ideas?
0
 
LVL 11

Assisted Solution

by:aaronakin
aaronakin earned 100 total points
ID: 22609604
Try this then...

SELECT SUM(cnt) cnt,
 MIN(cnt) min_cnt,
 MAX(cnt) max_cnt,
 AVG(cnt) avg_cnt
FROM
(SELECT dtcompleted, COUNT(*) as cnt
 FROM sds
 WHERE dtcompleted >= '20060626'
  AND dtcompleted < '20060628'
 GROUP BY dtcompleted
) T
0
 
LVL 22

Expert Comment

by:dportas
ID: 22609631
I think you must have run my query without the WITH ROLLUP clause. You should get the totals as well if ROLLUP is included.
0
 

Author Comment

by:drei22
ID: 22609658
Score! And yes you were right DPORTAS I fat fingered the cut and paste. lol Thanks a million guys as both scripts worked perfectly.
0
 

Author Closing Comment

by:drei22
ID: 31501727
A+ service here @ EE i can't believe I didn't sign up years ago!
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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