Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Query Help

Posted on 2008-09-30
11
Medium Priority
?
206 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
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 200 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 22

Accepted Solution

by:
dportas earned 400 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 400 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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 …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

971 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