Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 15th, 2004, 03:49 PM
lstrand lstrand is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 1 lstrand User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Help me compute average of a rowcount

I'm using MSSQL 2000. I'm trying to return a rounded integer value
for the average of the row count of a column.

Example data:
Value Rowcount
1 4
2 5
3 5
4 5

In the Oracle DB environment I would use:

select CEIL(avg(count(x)))
from TESTTABLE
groupby x

The result would be "5" (or "4.75" if I removed the CEIL function).

MSSQL doesn't like this SQL and I get an error:
Server: Msg 130, Level 15, State 1, Line 1
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

I rooted around for quite a while in SQL help docs and All I could come up with was the
COMPUTE clause. Is this wrong? Plus, it rounds down. Try the SQL:

CREATE TABLE #t(x INT)
INSERT INTO #t VALUES (1)
INSERT INTO #t VALUES (1)
INSERT INTO #t VALUES (1)
INSERT INTO #t VALUES (1)
INSERT INTO #t VALUES (2)
INSERT INTO #t VALUES (2)
INSERT INTO #t VALUES (2)
INSERT INTO #t VALUES (2)
INSERT INTO #t VALUES (2)
INSERT INTO #t VALUES (3)
INSERT INTO #t VALUES (3)
INSERT INTO #t VALUES (3)
INSERT INTO #t VALUES (3)
INSERT INTO #t VALUES (3)
INSERT INTO #t VALUES (4)
INSERT INTO #t VALUES (4)
INSERT INTO #t VALUES (4)
INSERT INTO #t VALUES (4)
INSERT INTO #t VALUES (4)
SELECT * FROM #t
SELECT count(x)
FROM #t
GROUP BY x
COMPUTE AVG (count(x))

The result is "4". I need the next greatest integer (round up). Try as I might with nested CAST
statements, I still couldn't get it to work.

Somebody out there has this SQL. All helping posts would be GREATLY appreciated.
Thanks in advance.

Kindly,
Loren


Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Help me compute average of a rowcount


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT