How do I combine two queries (union all) into one row?

后端 未结 3 512
灰色年华
灰色年华 2021-01-18 07:31

I have a tricky situation in trying to get information from multiple queries into a single row.

Consider the following table:

CpuUage:
    Time    ti         


        
3条回答
  •  梦谈多话
    2021-01-18 08:05

    I don't understand the problem with sub-querying, it seems like it should be just as fast:

    select
        sub.gn as groupname,
        sum(sub.nj) as NormJobs, sum(sun.nc) as NormCpu,
        sum(sub.sj) as Sys7Jobs, sum(sub.sc) as Sys7Cpu
      from (
          select
              groupname as gn,
              sum(jobs) as nj, sum(cpu) as nc,
              0 as sj, 0 as sc
            from tbl
              where subsys = 'NORM'
              group by groupname
            union all select
                groupname as gn,
                0 as nj, 0 as nc,
                sum(jobs) as sj, sum(cpu) as sc
              from tbl
              where subsys = 'SYS7'
              group by groupname
        ) as sub
        group by sub.gn
        order by 1
    

提交回复
热议问题