我无法计算有关mysql查询当前状态的总数据。
简而言之,我有3个相互关联的表。
m_shift_schedule site_shift_schedule m_ticket
================ =================== ============
* shift_id ======> * shift_id * ticket_id
* start_time * shift_date * ticket_status
* end_time * user_id ========> * ticket_served_by
我的查询:
SELECT user_id,
case when count(m_ticket.ticket_served_by)<2 then 'idle'
when count(m_ticket.ticket_served_by)=2 then 'Busy'
when count(m_ticket.ticket_served_by)>2 then 'Overload' end as status
FROM site_shift_schedule
LEFT JOIN m_shift_schedule ON site_shift_schedule.shift_id = m_shift_schedule.shift_id
LEFT JOIN m_ticket ON site_shift_schedule.user_id=m_ticket.ticket_served_by
WHERE site_shift_schedule.shift_date = '2019-02-11' and m_ticket.ticket_status in (4,5,6)
AND CURRENT_TIME > start_time and CURRENT_TIME < end_time
group by user_id
我从上面的查询中得到的输出:
user_id Status
============================= ============
ismail.rahman.saanin@random.co Idle
lutfi.aldi.nugroho@random.co Busy
问题是,如果我想让输出变得像这样,我该怎么办:
Status Total
==================================
Idle 1
Busy 1
答案 0 :(得分:2)
您可以在下面尝试-使用子查询
select status,count(user_id) as total
from
(
SELECT user_id,
case when count(m_ticket.ticket_served_by)<2 then 'idle'
when count(m_ticket.ticket_served_by)=2 then 'Busy'
when count(m_ticket.ticket_served_by)>2 then 'Overload' end as status
FROM site_shift_schedule
LEFT JOIN m_shift_schedule ON site_shift_schedule.shift_id = m_shift_schedule.shift_id
LEFT JOIN m_ticket ON site_shift_schedule.user_id=m_ticket.ticket_served_by
WHERE site_shift_schedule.shift_date = '2019-02-11' and m_ticket.ticket_status in (4,5,6)
AND CURRENT_TIME > start_time and CURRENT_TIME < end_time
group by user_id
)A group by status
答案 1 :(得分:0)
同样在group by
中的用例
SELECT
case when count(m_ticket.ticket_served_by)<2 then 'idle'
when count(m_ticket.ticket_served_by)=2 then 'Busy'
when count(m_ticket.ticket_served_by)>2 then 'Overload' end as status,
count(*) as total
FROM site_shift_schedule
LEFT JOIN m_shift_schedule ON site_shift_schedule.shift_id = m_shift_schedule.shift_id
LEFT JOIN m_ticket ON site_shift_schedule.user_id=m_ticket.ticket_served_by
WHERE site_shift_schedule.shift_date = '2019-02-11' and m_ticket.ticket_status in (4,5,6)
AND CURRENT_TIME > start_time and CURRENT_TIME < end_time
group by case when count(m_ticket.ticket_served_by)<2 then 'idle'
when count(m_ticket.ticket_served_by)=2 then 'Busy'
when count(m_ticket.ticket_served_by)>2 then 'Overload' end