how can I return multiple row counts

how can I return multiple row counts



I have three entities, User, Rule, and Activity



with the following relationship, One rule has many activities (bidirectional)
Many users have many activities(bidirectional), the server is with spring using spring jpa



I want to show how many Activities has one user started but not finished from every rule, with SQL this query would return that from one rule


SELECT COUNT(*) FROM user_activity WHERE user_id=userId AND score<10 AND
activity_id IN (SELECT id FROM activity WHERE rule_id=ruleId);



I could do that with JPQL, but how could I return that count from every rule?
so I could have from every rule, how many activities has that User



if someone needs it I can provide java model code too.



tables are User, Activity, Rule and User_Activity
User table



Activity table



Rule table



User Activity table






Could you show us the table structure? Without that it's impossible to guess at an answer. Some sample data would be helpful too...

– Nick
Sep 10 '18 at 4:08






added table estructure

– JoseCarlosPB
Sep 10 '18 at 4:21






hey, I deleted my answer as @nick already provided you with the working solution. I have no experience with translating queries to JPQL. We usually use MySQL syntax through hibernate without JPQL. Sorry :(

– Harly H.
Sep 10 '18 at 6:14





1 Answer
1



I think this query is probably what you want. It finds all the activities associated with a given rule, then joins the users participating in that activity who have not finished it (I presume that's what score<10 indicates?)


SELECT r.id, u.id, COUNT(a.id)
FROM rule r
JOIN activity a ON a.rule_id = r.id
JOIN user_activity ua ON ua.activity_id = a.id AND ua.score < 10
JOIN user u ON u.id = ua.user_id
GROUP BY r.id, u.id



Obviously you can expand the SELECT to include things like the rule description and the user name etc.


SELECT



I couldn't see any indication of what "activity complete" means so I've presumed it's that the score is less than 10. If that's not the case, you would need to change the join to user_activity appropriately.


user_activity






This works on mysql! now I'm trying to transform it into JPQL

– JoseCarlosPB
Sep 10 '18 at 5:30






That's great. Sorry I can't help with the JPQL if you want to leave the question unaccepted to try and get a JPQL solution I don't mind.

– Nick
Sep 10 '18 at 6:02



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)