我的表workitem
包含ticketNo
,owner
,status
,subject
和message
列,我想要每张票数用户处于每个状态。我的查询返回以下结果:
Owner Status Count
Daniel Open 2
Daniel InProgress 5
Daniel Completed 10
Moorthi Open 2
Moorthi InProgress 2
Moorthi OnHold 5
Moorthi Competed 7
但我正在寻找如下结果:
Owner Status Count
Daniel Open 3
Daniel InProgress 5
Daniel Completed 10
Daniel OnHold 0
Daniel Review 0
Moorthi Open 2
Moorthi InProgress 2
Moorthi OnHold 5
Moorthi Competed 7
Moorthi Review 0
我的查询:
select owner, Status, COUNT(status) from workitems
group by Owner, Status order by Owner, Status
答案 0 :(得分:0)
SELECT所有者,sum([Open])为[Open],sum([Closed])为[Closed],sum([InProgress])为[InProgress],sum([Review])为[Review] 来自工作项目 PIVOT(计数(状态) FOR status IN([Open],[Closed],[InProgress],[Review]))AS PVTTable 按所有者分组
sqlfiddle http://sqlfiddle.com/#!3/aa4d0/30
答案 1 :(得分:0)
尝试以下:
选择 TEMP1.owner, TEMP1.status, COUNT(w.subject) 从 ( 选择 DISTINCT所有者, TEMP.status 从 #workitem 交叉申请 (选择'打开' AS状态 UNION ALL SELECT' InProgress' UNION ALL SELECT'已完成' UNION ALL SELECT' OnHold' UNION ALL 选择'评论')AS TEMP )AS TEMP1 LEFT JOIN #workitem w 上 TEMP1.owner = w.owner AND TEMP1.status = w.status 通过...分组 TEMP1.owner, TEMP1.status 订购 TEMP1.owner