0

我有这个查询:

select MONTH(x.status_date) m
, YEAR(x.status_date) y
from [XREF_WET_MASTER] x
join F_ACCESSION_DAILY f
on x.accession_id=f.accession_id
pivot (count(*) for f.REQUISITION_TYPE in ([req1],[req2],[req3])
) as CountofThatTypeofReq

我收到了这个错误:

'*' 附近的语法不正确。

count(*)线

这是预期的结果:

+---+------+------+------+------+
| m |  y   | req1 | req2 | req3 |
+---+------+------+------+------+
| 1 | 2010 |   34 |    3 |    6 |
| 2 | 2010 |   56 |    6 |    0 |
| 3 | 2010 |    5 |   33 |    4 |
+---+------+------+------+------+

我将非常感谢有关此支点的任何指导!

4

2 回答 2

2

而不是count(*),您应该能够使用以下内容:

select *
from
(
  select MONTH(x.status_date) m
    , YEAR(x.status_date) y
    , f.REQUISITION_TYPE
    , f.accession_id
  from [XREF_WET_MASTER] x
  join F_ACCESSION_DAILY f
    on x.accession_id=f.accession_id
) src
pivot 
(
  count(REQUISITION_TYPE) 
  for REQUISITION_TYPE in ([req1],[req2],[req3])
) as CountofThatTypeofReq
于 2012-11-23T22:52:50.350 回答
1

根据文档,而不是 count(*),您似乎需要指定一个特定的列以进行透视,因此请尝试:

select MONTH(x.status_date) m
, YEAR(x.status_date) y
from [XREF_WET_MASTER] x
join F_ACCESSION_DAILY f
on x.accession_id=f.accession_id
pivot (count(f.accession_id) for f.REQUISITION_TYPE in ([req1],[req2],[req3])
) as CountofThatTypeofReq

...或类似的东西。

于 2012-11-23T22:51:46.910 回答