我无法计算有关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)
group by user_id
我从上面的查询中得到的输出:
user_id Status
============================= ============
ismail.rahman.saanin@random.co Idle
lutfi.aldi.nugroho@random.co Busy
tb.makkiy@yihuu.co Overload
问题是,如果我想让输出变得像这样,我该怎么办:
Idle 1
Busy 1
Overload 1
谢谢大家...
答案 0 :(得分:0)
使用子查询来获取此信息,如下所示:
SELECT status,count(user_id) as count 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)
group by user_id)
group by status
答案 1 :(得分:0)
一个简单的选择是只包装您当前的聚合查询,然后按status
计算列进行聚合:
SELECT
t.status,
COUNT(*) AS cnt
FROM
(
SELECT user_id,
CASE WHEN COUNT(mt.ticket_served_by) < 2 THEN 'idle'
WHEN COUNT(mt.ticket_served_by) = 2 THEN 'Busy'
WHEN COUNT(mt.ticket_served_by) > 2 THEN 'Overload' END AS status
FROM site_shift_schedule ss
LEFT JOIN m_shift_schedule ms ON ss.shift_id = ms.shift_id
LEFT JOIN m_ticket mt ON ss.user_id = mt.ticket_served_by
WHERE ss.shift_date = '2019-02-11' AND mt.ticket_status in (4,5,6)
GROUP BY user_id
) t
GROUP BY
t.status;
请注意,我在查询中引入了表别名,以使其更易于阅读。
答案 2 :(得分:0)
首先,您应该修复查询,以便它计算“ 0”行:
SELECT user_id,
(case when count(t.ticket_served_by) < 2 then 'idle'
when count(t.ticket_served_by) = 2 then 'Busy'
when count(t.ticket_served_by) > 2 then 'Overload'
end) as status
FROM site_shift_schedule sss LEFT JOIN
m_shift_schedule ss
ON sss.shift_id = ss.shift_id LEFT JOIN
m_ticket t
ON t.ticket_served_by = sss.user_id AND
t.ticket_status in (4, 5, 6)
WHERE sss.shift_date = '2019-02-11'
GROUP BY user_id;
否则,您的WHERE
将外部联接变为左联接。
有了这个,您可以通过附加的聚合级别获得所需的内容:
SELECT status, count(*)
FROM (SELECT user_id,
(case when count(t.ticket_served_by) < 2 then 'idle'
when count(t.ticket_served_by) = 2 then 'Busy'
when count(t.ticket_served_by) > 2 then 'Overload'
end) as status
FROM site_shift_schedule sss LEFT JOIN
m_shift_schedule ss
ON sss.shift_id = ss.shift_id LEFT JOIN
m_ticket t
ON t.ticket_served_by = sss.user_id AND
t.ticket_status in (4, 5, 6)
WHERE sss.shift_date = '2019-02-11'
GROUP BY user_id
) s
GROUP BY status;