Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old June 26th, 2003, 06:18 AM
Zoon Zoon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: UK
Posts: 5 Zoon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
SQL Query troubles

Using MS Access 2002.

Quote:
SELECT Count( case when ratings.rRatingId = 1 then 1 else 0 end ) as countNeg, Count( case when ratings.rRatingId = 2 then 1 else 0 end ) as countNeut, Count( case when ratings.rRatingId = 3 then 1 else 0 end ) as countPos FROM ratings INNER JOIN ratingNames ON ratingNames.rRatingId = ratings.rRatingId WHERE ratings.pId = " & pId & "


As far as I can tell that should work, but it throws up this error :

Quote:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'Count( case when ratings.rRatingId = 1 then 1 else 0 end )'.


What I am trying to do is count where rating = 1 for ratings.pId = & pId &, where rating = 2 for the same pId, and where rating = 3 for the same pId, all in the same query. I've done some googling and it should be possible using that but its throwing a fit !

This is the schema :

table : RATINGS
rId (primary key)
pId
rActive
rComment
rUser
rUserEmail
rRatingId (==ratingsNames.rRatingId)

table : RATINGSNAMES
rRatingId (primary key)
rRatingName

I am trying to avoid having to do three separate SQL queries against the database as, let's face it, Access can be slow enough anyway.

Can anyone show me the error of my ways ? Thanks in advance.

Reply With Quote
  #2  
Old June 26th, 2003, 06:32 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,310 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 4 h 20 m 43 sec
Reputation Power: 888
replace

case when ratings.rRatingId = 1 then 1 else 0 end

with

iif(ratings.rRatingId = 1, 1, 0)

microsoft access has its own sql language

rudy
http://r937.com/

Reply With Quote
  #3  
Old June 26th, 2003, 06:33 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
access does not support case expressions, you have to use the iif function instead.

e.g.
iif(rating=1,1,0)

Reply With Quote
  #4  
Old June 26th, 2003, 07:32 AM
Zoon Zoon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: UK
Posts: 5 Zoon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks guys, now using :

SELECT Count(iif(ratings.rRatingId = 1, 1, 0)) as countNeg, Count(iif(ratings.rRatingId = 2, 1, 0)) as countNeut, Count(iif(ratings.rRatingId = 3, 1, 0)) as countPos FROM ratings WHERE ratings.pId = " & pId

And its working perfectly.

I'll check around for some info on Access' own peculiar mix of SQL so I hopefully don't run into this again

Reply With Quote
  #5  
Old June 26th, 2003, 07:36 AM
Zoon Zoon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: UK
Posts: 5 Zoon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Oh no, its not working quite right. There are three total rows in the ratings table; I want it to only count on the condition that rRatingId = 1 ~ 3 as appropriate, which means countNeg, countNeut and countPos should (currently) be at 1, 1, 1 instead of 3, 3, 3.

Could I ask you to give me some input again ?

Reply With Quote
  #6  
Old June 26th, 2003, 08:28 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,310 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 4 h 20 m 43 sec
Reputation Power: 888
you probably want sum() instead of count()

think about it -- count three 1s or 0s, and how many of them are there? three

:-)

Reply With Quote
  #7  
Old June 27th, 2003, 05:10 AM
Zoon Zoon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: UK
Posts: 5 Zoon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Doesn't Sum() add together the values of the field, and Count() count the number of rows returned ?

Reply With Quote
  #8  
Old June 27th, 2003, 05:11 AM
Zoon Zoon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: UK
Posts: 5 Zoon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hmm evidently not as it seems to do what I want Thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > SQL Query troubles


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 4 hosted by Hostway