2

我有一个查询,用于获取一些字段项的计数:

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

不知道如何获得一排?

4

1 回答 1

0

我相信您的第一个查询可以简化如下:

SELECT
SUBSTRING(filename,5,9)AS pub, 
SUM(case when status = 2 then 1 else 0 end) as hold, 
SUM(case when status = 4 then 1 else 0 end) as ppf
FROM preflight_queue
GROUP BY SUBSTRING(filename,5,9)

之后,假设颜色和 p.status 之间存在一对一的关系,您的请求会变得更容易:

SELECT 
    SUBSTRING(p.filename,5,9)AS pub, 
    SUM(case when p.status = 2 then 1 else 0 end) as hold, 
    SUM(case when p.status = 4 then 1 else 0 end) as ppf, 
    MAX(case when s.id = 2 then s.forecolor else null end) as holdfc,
    MAX(case when s.id = 2 then s.backcolor else null end) as holdbc,
    MAX(case when s.id = 4 then s.forecolor else null end) as holdfc,
    MAX(case when s.id = 4 then s.backcolor else null end) as holdbc
FROM preflight_queue p
inner join preflight_status s on p.status = s.id
GROUP BY 
    SUBSTRING(filename,5,9)
于 2013-08-02T19:19:47.513 回答