Reputation: 11
I know the alias doesn't work but cant figure out to get get the intended result of "and count_of_circs = circ_limit"
select libraryid,
patronid,
count(circ.id) as count_of_circs,
(
select account.circsperpatronmonth
from account
where circ.libraryid = account.libraryid
) as circ_limit
from circ
where borrowed >= '2014-04-01'
and borrowed < '2014-04-19'
and count_of_circs = circ_limit
group by patronid,libraryid
Upvotes: 1
Views: 64
Reputation: 4694
You would need to add a line after "group by"
HAVING count_of_circs = circ_limit
It is not an alias, it is filtering after grouping.
Upvotes: 0
Reputation: 1270993
You want a having
clause:
select libraryid, patronid,
count(circ.id) as count_of_circs,
(select account.circsperpatronmonth
from account
where circ.libraryid=account.libraryid
) as circ_limit
From circ
where borrowed >= '2014-04-01' and borrowed < '2014-04-19'
group by patronid, libraryid
having count_of_circs = circ_limit;
Upvotes: 2