Solved

SQL Query Help

Posted on 2008-09-30
11
198 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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.…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 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