Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 18th, 2003, 02:22 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Query Question

Hello all

I tried to do a query on SQL Server as follows:

SELECT TYPE, Right([COURSEID],4) AS term, Sum(COUNTENR) AS enrolled, Sum(COUNTCEN) AS census, Sum(COUNTGRADED) AS graded, Sum(COUNT_W) AS withdraw
FROM dss_course COURSE
where (((TYPE)='OC' Or (TYPE)='DL') AND ((Right([COURSEID],4))='011S' Or (Right([COURSEID],4))='012S' Or (Right([COURSEID],4))='013S' Or (Right([COURSEID],4))='021S' Or (Right([COURSEID],4))='022S' Or (Right([COURSEID],4))='023S'))
GROUP BY TYPE, Right([coURSEID],4)
order by right(courseid,4), type
compute sum(countenr) by right([courseid],4)

It does not work, it output the error as

Server: Msg 8120, Level 16, State 1, Line 1
Column 'COURSE.COURSEID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'COURSE.COUNTENR' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

If I comment out the last line (the compute clause), I get results as:

DL 011S 866 665 593 169
OC 011S 577 362 438 37
DL 012S 2288 1436 1354 270
OC 012S 4238 2922 3045 317
DL 013S 2358 1715 1557 273
OC 013S 4125 2847 3066 212
DL 021S 1254 892 798 112
OC 021S 1702 921 1257 12

I add the compute clause because I need a summation by term. If the compute clause works, It should be like this.

DL 011S 866 665 593 169
OC 011S 577 362 438 37
        1443 ---> should be a sum of countenr
DL 012S 2288 1436 1354 270
OC 012S 4238 2922 3045 317

DL 013S 2358 1715 1557 273
OC 013S 4125 2847 3066 212

DL 021S 1254 892 798 112
OC 021S 1702 921 1257 12

I would appreciate an assistance from you guys.

Thank you.
Pong
  #2 (permalink)  
Old June 19th, 2003, 09:51 AM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello All

Why does nobody respond this question? Is it because of impossible? Please enlighten me if you have any idea. I really expect the idea.

Thank you for your help.
Pong
  #3 (permalink)  
Old June 19th, 2003, 10:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just so that you do not feel all alone and rejected, I have read your question, however I am by no means a SQL expert and I am not familiar with the COMPUTE clause.

It does look like a strange error if the statement works when you take out the COMPUTE clause. All I can suggest is to try GROUP BY COURSEID instead of Right([COURSEID],4).

Sorry I can not be of more assistance.

Regards
Owain Williams
  #4 (permalink)  
Old June 19th, 2003, 11:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Pong,

I think this might be your problem:

(From BOL)
If you use COMPUTE BY, you must also use an ORDER BY clause. The expressions must be identical to or a subset of those listed after ORDER BY, and must be in the same sequence. For example, if the ORDER BY clause is:

ORDER BY a, b, c

The COMPUTE clause can be any (or all) of these:

COMPUTE BY a, b, c
COMPUTE BY a, b
COMPUTE BY a

So either re-order the Group by, or add TYPE straight after the by in the 'compute ... by' clause.

BTW you could save yourself some effort by using

Code:
 Right([COURSEID],4))in ('011S' ,'012S','013S','021S','022S' ,'023S')
HTH

Chris


There are two secrets to success in this world:
1. Never tell everything you know


Similar Threads
Thread Thread Starter Forum Replies Last Post
Query Question Otacustes VB Databases Basics 6 July 15th, 2008 01:24 PM
sql query question ldp101068 SQL Server 2000 6 December 3rd, 2007 03:41 PM
Update Query Question arholly Access 2 January 19th, 2007 01:28 PM
query performance question kBusby Oracle 3 February 14th, 2005 04:42 PM
Database Query Question kbuska BOOK: Beginning ASP 3.0 4 September 23rd, 2004 11:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.