0

this works

SELECT COUNT(*) AS count ,transaction_id FROM loans_applicant  GROUP BY transaction_id

however i want to do this

SELECT COUNT(*) AS count ,transaction_id FROM loans_applicant  GROUP BY transaction_id WHERE count > 4

how can i do that ?

2
  • 3
    Change the where clause to a having clause. Commented Sep 16, 2015 at 11:12
  • THANKS - how do i mark as the accepted answer ? Commented Sep 16, 2015 at 11:25

3 Answers 3

2

That condition should go into HAVING clause rather like below. Moreover, you have placed the WHERE in wrong position; it should come right after FROM clause.

SELECT COUNT(*) AS count ,
transaction_id 
FROM loans_applicant  
GROUP BY transaction_id 
HAVING COUNT(*) > 4;
Sign up to request clarification or add additional context in comments.

Comments

1

Follow order as per below-

where
group by
having
order by

In this way your query will be-

SELECT COUNT(*) AS count ,transaction_id 
FROM loans_applicant 
GROUP BY transaction_id
having count > 4;

Comments

1

You can do two things for that.
First : Use HAVING clause.

  SELECT COUNT(*) AS count ,transaction_id FROM loans_applicant  GROUP BY transaction_id having count(*) > 4  

Second : Use Sub Query.

 SELECT count from (SELECT COUNT(*) AS count ,transaction_id FROM loans_applicant  GROUP BY transaction_id) where count > 4  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.