0

我查看了与数据透视相关的查询,但似乎找不到与我想做的匹配的查询。我有一张桌子如下: -

type  |  iscurrent
-----------------
A     |  1
A     |  1
A     |  0
B     |  1
B     |  1
B     |  1
C....

我想旋转这些数据,使其如下所示,iscurrent字段中只会有 0 和 1。

TYPE  |  1  |  0
-----------------
A     |  2  |  1
B     |  3  |  0
C     | ...

到目前为止我的代码是

SELECT loadid, '1', '0'
FROM (
SELECT iscurrent, loadid
FROM #table) up
PIVOT (sum(iscurrent) FOR iscurrent IN ([1], [0])) AS pvt
ORDER BY loadid
GO

尝试运行代码时出现一点操作数错误?

4

1 回答 1

1

尝试

SELECT loadid, [1], [0]

PIVOT (count(isCurrent) FOR iscurrent IN ([1], [0])) AS pvt

IE:

SELECT loadid, [1],[0]
FROM (  SELECT iscurrent, loadid FROM #table) up
PIVOT (count(iscurrent) FOR iscurrent IN ([1], [0])) AS pvt
ORDER BY loadid
于 2013-10-15T15:34:28.623 回答