TSQL分组字段

时间:2013-08-02 18:02:55

标签: sql-server tsql grouping

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

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)中,我需要获得两个额外的字段。(forecolor和backcolor)。

这两个表基于这两个字段有关系:

preflight_queue.status = preflight_staus.id

所以我尝试了以下内容:

  

SELECT(SUBSTRING(p.filename,5,9))AS pub,

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
然而,因为我现在必须将forecolor和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

不确定如何获得一行?

1 个答案:

答案 0 :(得分: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)