开发者

SQL query involving specific counts

person(id primary key, name)
money(acct primary key, loaner)
loan(id primary key, acct)

How would I create an SQL query that shows for each lo开发者_开发知识库aner the names of persons who took more than four loans from that specific loaner?

I tried count in the where clause but I am clueless so far.


 SELECT p.id, p.name, m.loaner, COUNT(*) FROM person p 
   INNER JOIN loan l ON p.id = l.id
   INNER JOIN money m ON l.acct = m.acct
   GROUP BY id, name, lower
   HAVING COUNT(*) > 4

What this does is create an aggregated record set with one record for each combination of id, name, and lender (loaner) along with a count of how many times that combination occurs.


you could use the HAVING clause. or write a subquery to get all the counts, and use WHERE count > 4 in the outer query

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜