在以下枢轴中重复行。如下图所示,Emp Name重复三次。如果我也使用订单,得到相同的结果。我做错了什么。我怎样才能得到正确的输出。谁能帮我吗。 (使用SQL Server 2012)
SELECT
*
FROM
(SELECT
e.EmpName, ClaimSource, TicketStatus
FROM
ClaimMaster c
INNER JOIN
Allocation a ON c.ClaimMasterId = a.ClaimMasterId
INNER JOIN
myUsers..EmpMaster e ON a.AllocatedToEmpId = e.AspUsername
WHERE
TicketStatus IN ('First Review','Calling Pending','Analyst Action','Recalling')
) m
PIVOT (
COUNT(ClaimSource)
FOR ClaimSource IN ([Aging Report],[Appeal],[Denied],[Rejected],[Special Project],[Eligibility],[Correspondence])
) n
PIVOT (
COUNT(TicketStatus)
FOR TicketStatus IN ([Recalling])
) o
期望的输出
如果我删除了TicketStatus
列&枢轴然后我可以得到所需的输出。
答案 0 :(得分:0)
如果我删除了TicketStatus列&枢轴,然后我可以得到所需的输出。
答案 1 :(得分:0)
首先我的建议是使用条件聚合,这样你就可以同时聚合这两个条件。
SELECT
e.EmpName
,COUNT(CASE WHEN ClaimSource = 'Aging Report' THEN ClaimSource END) as [Aging Report]
,COUNT(CASE WHEN ClaimSource = 'Appeal' THEN ClaimSource END) as [Appeal]
,COUNT(CASE WHEN ClaimSource = 'Denied' THEN ClaimSource END) as [Denied]
,COUNT(CASE WHEN ClaimSource = 'Rejected' THEN ClaimSource END) as [Rejected]
,COUNT(CASE WHEN ClaimSource = 'Special Project' THEN ClaimSource END) as [Special Project]
,COUNT(CASE WHEN ClaimSource = 'Eligibility' THEN ClaimSource END) as [Eligibility]
,COUNT(CASE WHEN ClaimSource = 'Correspondence' THEN ClaimSource END) as [Correspondence]
,COUNT(CASE WHEN TicketStatus = 'Recalling' THEN TicketStatus END) as [Recalling]
FROM ClaimMaster c
INNER JOIN Allocation a ON c.ClaimMasterId = a.ClaimMasterId
INNER JOIN myUsers..EmpMaster e ON a.AllocatedToEmpId = e.AspUsername
WHERE TicketStatus IN ('First Review','Calling Pending','Analyst Action','Recalling')
GROUP BY
e.EmpName
你基本上是在尝试基于ClaimSource和TicketStatus进行2种不同类型的聚合.PIVOT不能这样做。实际发生的事情您的第一个Pivot会创建额外的行,因为它还有票据状态。然后你的下一个枢轴,该列被旋转并计数,因此它将其关闭。但是因为所有其他列都存在,所以它实际上并不执行任何聚合。我只能从您的数据图像中假设您的员工没有任何RecallingTicketStatus。您可以进行第二轮聚合,而不是第二次转动:
SELECT
EmpName
,SUM([Aging Report]) as [Aging Report]
,SUM([Appeal] as [Appeal]
,SUM([Denied]) as [Denied]
,SUM[Rejected]) as [Rejected]
,SUM([Special Project]) as [Special Project]
,SUM([Eligibility]) as [Eligibility]
,SUM([Correspondence]) as [Correspondence]
,COUNT(Recalling) as Recalling
FROM (
SELECT e.EmpName, ClaimSource, TicketStatus FROM ClaimMaster c
INNER JOIN Allocation a ON c.ClaimMasterId = a.ClaimMasterId
INNER JOIN myUsers..EmpMaster e ON a.AllocatedToEmpId = e.AspUsername
WHERE TicketStatus IN ('First Review','Calling Pending','Analyst Action','Recalling')
) m
PIVOT (
COUNT(ClaimSource)
FOR ClaimSource IN ([Aging Report],[Appeal],[Denied],[Rejected],[Special Project],[Eligibility],[Correspondence])
) n
GROUP BY
EmpName
或许更容易只为ClaimSource做透视,并进行标准聚合和Recalling的连接。但如果你这样做,我建议改为携带员工ID。
SELECT *
FROM
(SELECT *
FROM (
SELECT e.EmpName, ClaimSource FROM ClaimMaster c
INNER JOIN Allocation a ON c.ClaimMasterId = a.ClaimMasterId
INNER JOIN myUsers..EmpMaster e ON a.AllocatedToEmpId = e.AspUsername
WHERE TicketStatus IN ('First Review','Calling Pending','Analyst Action')
) m
PIVOT (
COUNT(ClaimSource)
FOR ClaimSource IN ([Aging Report],[Appeal],[Denied],[Rejected],[Special Project],[Eligibility],[Correspondence])
) n
) t
INNER JOIN (
SELECT
SELECT e.EmpName, COUNT(TicketStatus) as Recalling FROM ClaimMaster c
INNER JOIN Allocation a ON c.ClaimMasterId = a.ClaimMasterId
INNER JOIN myUsers..EmpMaster e ON a.AllocatedToEmpId = e.AspUsername
WHERE TicketStatus IN ('Recalling')
) t2
ON T1.EmpName = t2.EmpName
要在将来逐步解决查询的每个部分,请解决此问题。因此,使用1个数据透视看结果。有了TicketStatus,如果你这样做,你应该举起一面旗帜。