按时间在同一个表上对多个计数查询进行分组

时间:2018-02-22 12:26:54

标签: mysql sql time group-by count

我有一个查询,其中我需要3列,以及一个额外的'时间'列,我需要做的是运行每个计数查询并将它们匹配到相关的时间,但是发生的是查询是什么返回计数总和并将其添加到每次。见下文。 enter image description here

应该发生的事情是我得到一个这样的列表:

enter image description here

我知道我最终分组的方式,但我似乎无法将这些内容归为一类,任何人都有任何想法?

SELECT
case 
when ( extract( HOUR FROM e.started) = 0 ) then '12am'
when ( extract( HOUR FROM e.started) < 12) then concat(extract(HOUR FROM e.started), 'am') 
when ( extract( HOUR FROM e.started) = 12) then '12pm'
when ( extract( HOUR FROM e.started) > 12) then concat( ( extract(HOUR FROM e.started) -12 ), 'pm') 
end  AS "Time", 
(
    SELECT COUNT(*)
    FROM eventinfo e
    join tickets o on e.ticket_id = o.id
    where e.created > '2018-02-20 00:00:00' and e.created < '2018-02-21 00:00:00'
        and o.stagename = ’Stage3'
) AS 'Column1',
(
    SELECT COUNT(*)
    FROM eventinfo e
    join tickets o on e.ticket_id = o.id
    where e.created > '2018-02-20 00:00:00' and e.created < '2018-02-21 00:00:00'
    and o.stagename = ’Stage2'

) AS 'Column2',
(
    SELECT COUNT(*)
    FROM eventinfo e
    join tickets o on e.ticket_id = o.id
    where e.created > '2018-02-20 00:00:00' and e.created < '2018-02-21 00:00:00'
    and o.stagename = ’Stage1'
) AS 'Column3'
FROM eventinfo e
group by extract(hour from e.started)

编辑: 如果我从每个计数查询中查询时间,我会收到错误:“操作数应该包含1列”

    SELECT
(
    SELECT 
    case 
    when ( extract( HOUR FROM e.started_at) = 0 ) then '12am'
    when ( extract( HOUR FROM e.started_at) < 12) then concat(extract(HOUR FROM e.started_at), 'am') 
    when ( extract( HOUR FROM e.started_at) = 12) then '12pm'
    when ( extract( HOUR FROM e.started_at) > 12) then concat( ( extract(HOUR FROM e.started_at) -12 ), 'pm') 
    end  AS "Time", 
    COUNT(*)
    FROM events e
    join operations o on e.operation_id = o.id
where e.created_at > '2018-02-20 00:00:00' and e.created_at < '2018-02-21 00:00:00'
    and o.stagename = ’Stage1'
) AS 'Column1',
(
    SELECT 
    case 
    when ( extract( HOUR FROM e.started_at) = 0 ) then '12am'
    when ( extract( HOUR FROM e.started_at) < 12) then concat(extract(HOUR FROM e.started_at), 'am') 
    when ( extract( HOUR FROM e.started_at) = 12) then '12pm'
    when ( extract( HOUR FROM e.started_at) > 12) then concat( ( extract(HOUR FROM e.started_at) -12 ), 'pm') 
    end  AS "Time", 
    COUNT(*)
    FROM events e
    join operations o on e.operation_id = o.id
where e.created_at > '2018-02-20 00:00:00' and e.created_at < '2018-02-21 00:00:00'
    and o.stagename = ’Stage2'

) AS 'Column2',
(
    SELECT 
    case 
    when ( extract( HOUR FROM e.started_at) = 0 ) then '12am'
    when ( extract( HOUR FROM e.started_at) < 12) then concat(extract(HOUR FROM e.started_at), 'am') 
    when ( extract( HOUR FROM e.started_at) = 12) then '12pm'
    when ( extract( HOUR FROM e.started_at) > 12) then concat( ( extract(HOUR FROM e.started_at) -12 ), 'pm') 
    end  AS "Time", 
    COUNT(*)
    FROM events e
    join operations o on e.operation_id = o.id
    where e.created_at > '2018-02-20 00:00:00' and e.created_at < '2018-02-21 00:00:00'
    and o.stagename = ’Stage3'
) AS 'Column3'

1 个答案:

答案 0 :(得分:2)

您查询相同的来源太多次了。一切都可以一次完成。有关更具体的答案,请指定您正在使用的RDBMS以及有关所需输出的更多信息,因为您提供的信息显然不是查询结果。

试试这个:

SELECT
CONCAT(TIME_FORMAT(e.started, "%h"), TIME_FORMAT(e.started, "%p")) AS "Time", 

sum(case when  o.stagename = 'Stage3' then 1 else 0 end) as 'Column1',
sum(case when  o.stagename = 'Stage2' then 1 else 0 end) AS 'Column2',
sum(case when  o.stagename = 'Stage1' then 1 else 0 end) AS 'Column3'

FROM eventinfo e
    join tickets o on e.ticket_id = o.id

group by extract(hour from e.started)