dbTalk Databases Forums  

Count wrong on SQL statement

comp.databases.postgresql comp.databases.postgresql


Discuss Count wrong on SQL statement in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Bigjim
 
Posts: n/a

Default Count wrong on SQL statement - 01-26-2007 , 12:25 PM






I have a sql query that does not provide the correct counts.

select location.name as "location Name",
count ( case when incident.location_type = 138 AND
incident_detail.witnessed = TRUE then 1 else null end) as "Fall
witnessed"
from incident, incident_detail, location
where location.id = incident.id

The is only 1 incident in my test DB that has a
incident_detail.witnessed set to true, so the outut should be

location name | Fall Witnessed
==================================
Floor | 0
Room | 0
Hallway | 1


But I get instead ;

location name | Fall Witnessed
==================================
Floor | 1
Room | 1
Hallway | 1

What am I doing wrong here?
Is it becasue the query is from mutlitple tables?

Let me know i you can help

Thanks


Reply With Quote
  #2  
Old   
HansH
 
Posts: n/a

Default Re: Count wrong on SQL statement - 01-26-2007 , 06:04 PM






"Bigjim" <bigjim.still (AT) rogers (DOT) com> schreef in bericht
news:1169835949.275145.213640 (AT) q2g2000cwa (DOT) googlegroups.com...
Quote:
I have a sql query that does not provide the correct counts.

select location.name as "location Name",
count ( case when incident.location_type = 138 AND
incident_detail.witnessed = TRUE then 1 else null end) as "Fall
witnessed"
from incident, incident_detail, location
where location.id = incident.id

Is it becasue the query is from mutlitple tables?
You might be right there ...
adding ' and incident.id = incident_detail.id'
may cure

To avoid such unnoticed drop-outs of code, consider using explicite join
syntax
from incident
join location on( incident.id = location.id )
join incident_detail on( incident.id = incident_detail.id )


HansH




Reply With Quote
  #3  
Old   
Benjamin Gufler
 
Posts: n/a

Default Re: Count wrong on SQL statement - 01-28-2007 , 04:55 AM



Hi,

On 2007-01-26 19:25, Bigjim wrote:
Quote:
I have a sql query that does not provide the correct counts.
[...]
count ( case when incident.location_type = 138 AND
incident_detail.witnessed = TRUE then 1 else null end)
[...]

Probably you should try to use "sum" instead of "count".

hth,
Benjamin


Reply With Quote
  #4  
Old   
Bigjim
 
Posts: n/a

Default Re: Count wrong on SQL statement - 01-28-2007 , 09:15 PM



Thanks HansH !, that is exactly what it was.
I learn something new everyday, and that's what keeps me young !!

On Jan 26, 7:04 pm, "HansH" <h... (AT) invalid (DOT) invalid> wrote:
Quote:
"Bigjim" <bigjim.st... (AT) rogers (DOT) com> schreef in berichtnews:1169835949.275145.213640 (AT) q2g2000cwa (DOT) googlegroups.com...>I have a sql query that does not provide the correct counts.

select location.name as "location Name",
count ( case when incident.location_type = 138 AND
incident_detail.witnessed = TRUE then 1 else null end) as "Fall
witnessed"
from incident, incident_detail, location
where location.id = incident.id

Is it becasue the query is from mutlitple tables?You might be right there ...
adding ' and incident.id = incident_detail.id'
may cure

To avoid such unnoticed drop-outs of code, consider using explicite join
syntax
from incident
join location on( incident.id = location.id )
join incident_detail on( incident.id = incident_detail.id )

HansH


Reply With Quote
Reply




Thread Tools
Display Modes

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.