Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why can't I do this?

Status
Not open for further replies.

SMAlvarez

Programmer
Dec 4, 2005
27
US
Why is this illegal and how can I fix it? Thanks.

SELECT COUNT(*) AS NumberOfHistory
FROM Enrolls
WHERE COURSENUM = 405;

SELECT COUNT(*) AS NumberOfScience
FROM Enrolls
WHERE COURSENUM = 605;
 
Possibly because you are counting everything ie (*)

Try counting your table key (whatever its name is)
eg

SELECT COUNT(table_key) AS NumberOfHistory
FROM Enrolls
WHERE COURSENUM = 405;

Or are you wanting two columns then

select sum( case when coursenum = 605 then 1 else 0 end ) as NumOfSci,
sum( case when coursenum = 405 then 1 else 0 end ) as NumOfHist
FROM Enrolls

Ian
 
Or are you trying to get 2 rows, in which case use a union
Code:
SELECT "History" AS Course, COUNT(*) AS Number
FROM Enrolls
WHERE COURSENUM = 405
UNION
SELECT "Science" AS Cource COUNT(*) AS Number
FROM Enrolls
WHERE COURSENUM = 605;

Or more efficiently:
Code:
SELECT COURSENUM, COUNT(*)
FROM Enrolls
WHERE COURSENUM IN (405,605)
GROUP BY COURSENUM

Without knowing a little more about what you want, what error you're getting and what system you're running, it's difficult to provide you with the exact answer you need!

hth

Ben

----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
Why is this illegal and how can I fix it?

I agree with r937. If it's "illegal", there must be an error message you can post?

Ever notice how fast Windows runs? Me neither.
 
plz try this..

SELECT COURSENUM,COUNT(*) AS NumberOfScience
FROM Enrolls
WHERE COURSENUM = 605;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top