0

Im attempting to get some records from a table based on certain factors.

One of the factors is simply with fields on the same table, the other is when joining to another table, I want to compare the number of records in the joined table to a field on the first table. Below is a sample code.

select * from tDestinations D 
left join tLiveCalls LC on LC.DestinationID = D.ID 
where D.ConfigurationID = 1486 
AND (D.Active = 1 AND D.AlternateFail > GETDATE())
-- Having COUNT(LC.ID) = D.Lines

Now from the code above I cant have the Count function in the where clause, and I cant have a field in in the having clause without it being in a function.

Im probably missing something very simple here. But I cant figure it out.

Any help is appreciated it.

EDIT: I do apologise should have explained the structure of the tables, the Destinations are single records, which the LiveCalls table can hold multiple records based on the Destinations ID (foreign key).

Thank you very much for everyones help. My final code:

select D.ID, D.Description, D.Lines, D.Active, D.AlternateFail, D.ConfigurationID, COUNT(LC.ID) AS LiveCalls from tDestinations D 
left join tLiveCalls LC on LC.DestinationID = D.ID 
where D.ConfigurationID = @ConfigurationID
AND (D.Active = 1 AND D.AlternateFail > GETDATE())
GROUP BY D.ID, D.Description, D.Lines, D.Active, D.AlternateFail, D.ConfigurationID
HAVING COUNT(LC.ID) <= D.Lines
Valeklosse
  • 1,017
  • 3
  • 19
  • 36
  • `WHERE` deals with rows, `HAVING` deals with groups, see this: http://stackoverflow.com/a/1131076/65223 Aggregate functions work on groups. As a result, they need to be in the `HAVING` and not the `WHERE` – KM. Apr 11 '12 at 15:38

2 Answers2

2

The simple thing you're missing is the GROUP BY statement.

As JNK mentioned in the comments below, you cannot use an aggregate function (such as COUNT, AVG, SUM, MIN) if you don't have a GROUP BY clause, unless your SELECT statement only references literal values (and no column names).

Your code should probably be something like:

SELECT <someFields>
FROM tDestinations D 
LEFT JOIN tLiveCalls LC on LC.DestinationID = D.ID 
WHERE D.ConfigurationID = 1486 
AND (D.Active = 1 AND D.AlternateFail > GETDATE())
GROUP BY <someFields>
HAVING COUNT(LC.ID) = D.Lines

Note that you have to specify the selected fields explicitely, in both the SELECT and GROUP BY statements (no * allowed).

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
1

you can only use having with aggregations. Actually having is the "where clause" for aggregation, BUT you can still have a where on the columns that you are no aggregating.

For example:

SELECT TABLE_TYPE, COUNT(*)
FROM INFORMATION_SCHEMA.TABLES
where TABLE_TYPE='VIEW'
group by TABLE_TYPE
having COUNT(*)>1

In your case you need to use havving count(*)=1 so, I think your query would be something like this:

select YOUR_COLUMN 
from tDestinations D 
left join tLiveCalls LC on LC.DestinationID = D.ID 
where D.ConfigurationID = 1486 AND (D.Active = 1 AND D.AlternateFail > GETDATE())
group by YOUR_COLUMN
Having COUNT(LC.ID) = value
Diego
  • 34,802
  • 21
  • 91
  • 134