Reputation: 23
i have the following query
SELECT c.`name` as categories,times_booked.booked as booked
FROM req_profiles rq
inner join categories as c on c.id = rq.category_id
left join
(
SELECT rq.id, COUNT(rq.id) as booked
FROM req_profiles as rq
inner join profile_matchings as pm on pm.req_profile_id = rq.id
WHERE pm.`status` = 'booked'
AND rq.user_id = 736
AND (pm.created_at BETWEEN '2018-01-01 00:00' AND '2019-02-13 00:00')
GROUP BY rq.id
) as times_booked on times_booked.id = rq.id
where rq.user_id=736
and rq.`status` = 'active'
ORDER BY times_booked.booked,rq.id desc
limit 5
this is what i get:
categories| booked
-------------------
Talent NULL
Talent 1
Talent 1
but i would like to get the result something like this:
categories| booked
-------------------
Talent 2
Thanks for the help!
Upvotes: 2
Views: 39
Reputation: 133360
use your query as a subquery anf group by
select categories, count(booked)
from (
SELECT c.`name` as categories,times_booked.booked as booked
FROM req_profiles rq
inner join categories as c on c.id = rq.category_id
left join
(
SELECT rq.id, COUNT(rq.id) as booked
FROM req_profiles as rq
inner join profile_matchings as pm on pm.req_profile_id = rq.id
WHERE pm.`status` = 'booked'
AND rq.user_id = 736
AND (pm.created_at BETWEEN '2018-01-01 00:00' AND '2019-02-13 00:00')
GROUP BY rq.id
) as times_booked on times_booked.id = rq.id
where rq.user_id=736
and rq.`status` = 'active'
ORDER BY times_booked.booked,rq.id desc
limit 5
) t
group by categories
Upvotes: 2
Reputation: 153
select x.a, count(x.b)
from (
select 'a' a, 10 b from dual
union all
select 'a' a, 20 from dual
union all
select 'b' a, 10 from dual
)x
Hope it helps!
Upvotes: 0