Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
influent
Constraint Violating Yak Guru
367 Posts |
Posted - 2004-11-11 : 16:06:32
|
| I'm trying to get the rowcount from a query, but instead of the query returning one value (which should be 42 at the moment) it returns 42 rows, all containing the value 1. ???Here's my query:SELECT COUNT(*)FROM Incident INNER JOIN (Incident_Contacts INNER JOIN Contact ON Incident_Contacts.ContactID = Contact.ContactID) ON Incident.IncidentID = Incident_Contacts.IncidentIDGROUP BY Incident.IncidentID, Incident.ProjectName, Contact.ContactType, Incident.Status, Incident.PriorityHAVING (((Incident.ProjectName)='/Pro') AND ((Contact.ContactType) = 'Customer') AND ((Incident.Priority)='High' Or (Incident.Priority)='Management Attention')) OR (((Incident.ProjectName)='/Technical Support') AND ((Contact.ContactType) = 'Customer') AND ((Incident.Status) NOT LIKE '%Closed%' And (Incident.Status) NOT LIKE '%Released%')); |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-11-11 : 16:16:26
|
read up on GROUP BY -- you don't want that if you want to return a single count in your query. GROUP BY causes groups to occur in your resultset.then, after removing the GROUP BY, you need to change your HAVING clause into a WHERE clause. you put criteria for a query with no grouping (or before grouping) in a WHERE clause -- you put criteria for calculations done AFTER grouping in a HAVING clause.SELECT COUNT(*)FROM IncidentINNER JOIN (Incident_Contacts INNER JOIN Contact ON Incident_Contacts.ContactID = Contact.ContactID)ON Incident.IncidentID = Incident_Contacts.IncidentIDGROUP BY Incident.IncidentID, Incident.ProjectName, Contact.ContactType, Incident.Status, Incident.PriorityHAVING WHERE (((Incident.ProjectName)='/Pro') AND ((Contact.ContactType) = 'Customer')AND ((Incident.Priority)='High'Or (Incident.Priority)='Management Attention'))OR (((Incident.ProjectName)='/Technical Support')AND ((Contact.ContactType) = 'Customer')AND ((Incident.Status) NOT LIKE '%Closed%' And (Incident.Status) NOT LIKE '%Released%'));- Jeff |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-11-11 : 16:16:52
|
If you want to count the records like that then you have to perform the count on the query results. ie.Select count(*) From ( SELECT Incident.IncidentID, Incident.ProjectName, Contact.ContactType, Incident.Status, Incident.Priority FROM Incident INNER JOIN (Incident_Contacts INNER JOIN Contact ON Incident_Contacts.ContactID = Contact.ContactID) ON Incident.IncidentID = Incident_Contacts.IncidentID GROUP BY Incident.IncidentID, Incident.ProjectName, Contact.ContactType, Incident.Status, Incident.Priority HAVING (((Incident.ProjectName)='/Pro') AND ((Contact.ContactType) = 'Customer') AND ((Incident.Priority)='High' Or (Incident.Priority)='Management Attention')) OR (((Incident.ProjectName)='/Technical Support') AND ((Contact.ContactType) = 'Customer') AND ((Incident.Status) NOT LIKE '%Closed%' And (Incident.Status) NOT LIKE '%Released%')) ) A Corey |
 |
|
|
AndyB13
Aged Yak Warrior
583 Posts |
Posted - 2004-11-11 : 16:20:42
|
| If you only want a total count, why use GROUP BY as this will summarise the function by each group, which in your case is 1Andy |
 |
|
|
influent
Constraint Violating Yak Guru
367 Posts |
Posted - 2004-11-11 : 16:25:16
|
| I'm not really sure why the GROUP BY is in there, I inherited the query from a third-party application. I figured out that if I throw it into a view and SELECT COUNT(*) on the view, I get what I'm looking for. Jeff's suggestion seemed good, but I get a count of 47 from it instead of 42. (??) And I get errors from Corey's suggestion:Server: Msg 156, Level 15, State 1, Line 7Incorrect syntax near the keyword 'GROUP'.Server: Msg 170, Level 15, State 1, Line 10Line 10: Incorrect syntax near 'Incident'.Server: Msg 170, Level 15, State 1, Line 15Line 15: Incorrect syntax near 'Incident'.thanks, Alex |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-11-11 : 17:14:21
|
I just copied your query.... What did you try to run exactly?? DOes the original query error?Corey |
 |
|
|
influent
Constraint Violating Yak Guru
367 Posts |
Posted - 2004-11-11 : 17:38:32
|
| All you did was add two parentheses, right? The original gives no errors. Don't worry too much about it for my sake (only for posterity if you wish). |
 |
|
|
|
|
|
|
|