Solved

SQL Query Help

Posted on 2008-09-30
11
196 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 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

15 Experts available now in Live!

Get 1:1 Help Now