Solved

SQL Query Help

Posted on 2008-09-30
11
197 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Caste datetime 2 50
Help with SQL field formatting 3 20
sql query help 4 44
subtract 1 in Access 2003 query 7 35
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

947 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

17 Experts available now in Live!

Get 1:1 Help Now