将Access交叉表查询转换为T-SQL等效项

时间:2013-07-16 15:55:51

标签: tsql ms-access pivot transform crosstab

TRANSFORM Count(qryEAOCalls.CALLID) AS CountOfCALLID
SELECT qryEAOCalls.TAPSTAFFNAME, Count(qryEAOCalls.CALLID) AS [Total Calls]
FROM qryEAOCalls
WHERE qryEAOCalls.CALLDATE Between #1/1/1900# And Date()
GROUP BY qryEAOCalls.TAPSTAFFNAME
PIVOT qryEAOCalls.Status In ("Unassigned","Open","Closed","Follow-up Needed");

如何将其转换为等效的T-SQL?

1 个答案:

答案 0 :(得分:1)

您应该可以使用类似于以下内容的内容:

select TAPSTAFFNAME,
  Unassigned, Open, Closed, [Follow-up Needed],
  TotalCalls
from
(
  select e.TAPSTAFFNAME,
    e.CALLID,
    e.Status, 
    count(*) over(partition by e.TAPSTAFFNAME) TotalCalls
  from qryEAOCalls e
  where e.CALLDATE >= '1900-01-01'
    and e.CALLDATE <= getdate()
) src
pivot
(
  count(CALLID)
  for status in (Unassigned, Open, Closed, [Follow-up Needed])
) piv