我有一个查询,用于获取一些字段项的计数:
SELECT (SUBSTRING(p.filename,5,9))AS pub,
COUNT(hc.queue)AS Hold,
COUNT(pff.queue)AS ppf,
FROM Preflight_Queue p
LEFT OUTER JOIN (SELECT status,COUNT(*)AS queue FROM Preflight_Queue WHERE status = 4 GROUP BY status)hc on hc.status= p.status
LEFT OUTER JOIN (SELECT status,COUNT(*)AS queue FROM Preflight_Queue WHERE status = 2 GROUP BY status)pff on pff.status= p.status
group by SUBSTRING(P.filename,5,9)
这给出了这样的记录集:
pub Hold pff
RET-S0313 14 14
ABC-X0313 20 5
但是在一个单独的表(Preflight_Status)中,我需要获得两个额外的字段。(前景色和背景色)。
这两个表具有基于以下两个字段的关系:
preflight_queue.status = preflight_staus.id
所以我尝试了以下操作:
选择(SUBSTRING(p.filename,5,9))作为酒吧,
COUNT(hc.queue)AS Hold,(hc.forecolor)as holdfc,(hc.backcolor) AS holdbc, COUNT(pff.queue)AS pff,(pff.forecolor)as pfffc,(pff.backcolor) AS pffbc FROM Preflight_Queue p LEFT OUTER JOIN ( SELECT p.status,s.forecolor,s.backcolor,COUNT(*)AS queue FROM Preflight_Queue p JOIN preflight_Status s on s.id = p.status WHERE p.status = 4 GROUP BY p.status,s.forecolor,s.backcolor )hc on hc.status= p.status LEFT OUTER JOIN ( SELECT p.status,s.forecolor,s.backcolor,COUNT(*)AS queue FROM Preflight_Queue p JOIN preflight_Status s on s.id = p.status WHERE p.status = 2 GROUP BY p.status,s.forecolor,s.backcolor )pff on pff.status= p.status group by SUBSTRING(P.filename,5,9),hc.forecolor,hc.backcolor,pff.forecolor,pff.backcolor
但是,因为我现在必须对前景色和背景色字段进行分组。我得到这个结果:
pub Hold pff holdfc holdbc pfffc pffbc RET-S0313 14 14 null null null null RET-S0313 null null 0x000000 0xFF99FF null null ABC-X0313 20 5 null null null null ABC-X0313 null null null null 0x000000 0xFFCCFF
我想要做的是将它们全部放在同一行,如下所示:
pub Hold pff holdfc holdbc pfffc pffbc RET-S0313 14 14 0x000000 0xFF99FF 0x000000 0x000000 ABC-X0313 20 5 0x000000 0xFF99FF 0x000000 0xFFCCFF
不知道如何获得一排?