SQL Server PIVOT查询"转换失败"

时间:2014-09-05 21:11:38

标签: sql sql-server tsql

目前我有这种格式的数据

Current Format

我想用这种格式

Account AccountUnit Description  ATL  BOS   
1111      10           Trucking  23   50      
2222      13           Banking   34   21    

我的查询如下

DECLARE 
@FISCAL_YEAR AS NVARCHAR(4),
@PERIOD AS VARCHAR(2),
@COL AS NVARCHAR(MAX),
@SQL AS NVARCHAR(MAX)

SELECT @COL= COALESCE (@COL+ ',','')+QUOTENAME(MX_VALUE)FROM
(SELECT DISTINCT MX_VALUE FROM AUMXVALUE WHERE MATRIX_CAT='_DIV') AS A

SET @FISCAL_YEAR = 2014
SET @PERIOD=6


SET @SQL=
N'
SELECT CURR.COMPANY, CURR.ACCT_UNIT, CURR.DESCRIPTION, CURR.ACCOUNT,
(
(SELECT UNITS.* , GLN.DESCRIPTION FROM
(SELECT 
GLU.COMPANY,
GLU.ACCT_UNIT,
GLU.ACCOUNT,
AMV.MATRIX_CAT,
'+@COL+',
CASE 
WHEN @PERIOD=1 THEN GLU.DB_UNITS_01+GLU.CR_UNITS_01 
WHEN @PERIOD=2 THEN GLU.DB_UNITS_02+GLU.CR_UNITS_02 
WHEN @PERIOD=3 THEN GLU.DB_UNITS_03+GLU.CR_UNITS_03
WHEN @PERIOD=4 THEN GLU.DB_UNITS_04+GLU.CR_UNITS_04 
WHEN @PERIOD=5 THEN GLU.DB_UNITS_05+GLU.CR_UNITS_05 
WHEN @PERIOD=6 THEN GLU.DB_UNITS_06+GLU.CR_UNITS_06 
WHEN @PERIOD=7 THEN GLU.DB_UNITS_07+GLU.CR_UNITS_07 
WHEN @PERIOD=8 THEN GLU.DB_UNITS_08+GLU.CR_UNITS_08 
WHEN @PERIOD=9 THEN GLU.DB_UNITS_09+GLU.CR_UNITS_09
WHEN @PERIOD=10 THEN GLU.DB_UNITS_10+GLU.CR_UNITS_10
WHEN @PERIOD=11 THEN GLU.DB_UNITS_11+GLU.CR_UNITS_11 
WHEN @PERIOD=12 THEN GLU.DB_UNITS_12+GLU.CR_UNITS_12 
ELSE 0 END AS ACT
FROM GLUNITS GLU JOIN GLNAMES GLN 
ON GLU.COMPANY=GLN.COMPANY
AND GLU.ACCT_UNIT='''+100+'''
JOIN AUMXVALUE AMV
ON GLN.OBJ_ID=AMV.OBJ_ID
AND AMV.MATRIX_CAT='''+'_DIV'+'''
AND GLU.FISCAL_YEAR=@FISCAL_YEAR
AND GLU.ACCOUNT=9100
)UNITS
JOIN GLNAMES GLN
ON UNITS.COMPANY=GLN.COMPANY
AND UNITS.ACCT_UNIT=GLN.ACCT_UNIT) CURR
) T PIVOT( SUM(ACT) FOR MX_VALUE IN (' + @COL + ')) AS PIVTBL'


EXEC (@SQL)

但我一直收到这个错误

Conversion failed when converting the nvarchar value '

SELECT CURR.COMPANY, CURR.ACCT_UNIT, CURR.DESCRIPTION, CURR.ACCOUNT,
(
(SELECT UNITS.* , GLN.DESCRIPTION FROM
(SELECT 
GLU.COMPANY,
GLU.ACCT_UNIT,
GLU.ACCOUNT,
AMV.MATRIX_CAT,
[ATL                             ],[BOS                             ]
CASE 
    WHEN @PERIOD=1 THEN GLU.DB_UNITS_01+GLU.CR_UNITS_01 
    WHEN @PERIOD=2 THEN GLU.DB_UNITS_02+GLU.CR_UNITS_02 
    WHEN @PERIOD=3 THEN GLU.DB_UNITS_03+GLU.CR_UNITS_03
WHEN @PERIOD=4 THEN GLU.DB_UNITS_04+GLU.CR_UNITS_04 
WHEN @PERIOD=5 THEN GLU.DB_UNITS_05+GLU.CR_UNITS_05 
WHEN @PERIOD=6 THEN GLU.DB_UNITS_06+GLU.CR_UNITS_06 
WHEN @PERIOD=7 THEN GLU.DB_UNITS_07+GLU.CR_UNITS_07 
WHEN @PERIOD=8 THEN GLU.DB_UNITS_08+GLU.CR_UNITS_08 
WHEN @PERIOD=9 THEN GLU.DB_UNITS_09+GLU.CR_UNITS_09
WHEN @PERIOD=10 THEN GLU.DB_UNITS_10+GLU.CR_UNITS_10
WHEN @PERIOD=11 THEN GLU.DB_UNIT...

有什么想法吗?感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

你需要像VARCHAR一样强制转换100

...
AND GLU.ACCT_UNIT='''+CAST(100 AS VARCHAR(3))+'''
...

或者只是将其添加为如下字符串:

...
AND GLU.ACCT_UNIT='''+'100'+'''
...

答案 1 :(得分:0)

感谢PM77-1和gotqn

我尝试了你的建议,现在我明白了:必须声明标量变量" @ PERIOD"。

根据您的建议,这是我修改过的代码:

DECLARE 
@FISCAL_YEAR AS VARCHAR(4),
@PERIOD AS VARCHAR(2),
@COL AS VARCHAR(MAX),
@SQL AS VARCHAR(MAX)

SELECT @COL= COALESCE (@COL+ ',','')+QUOTENAME(MX_VALUE)FROM
(SELECT DISTINCT MX_VALUE FROM AUMXVALUE WHERE MATRIX_CAT='_DIV') AS A

SET @FISCAL_YEAR = 2014
SET @PERIOD=6


  SET @SQL=
N'
SELECT CURR.COMPANY, CURR.ACCT_UNIT, CURR.DESCRIPTION, CURR.ACCOUNT,
(
(SELECT UNITS.* , GLN.DESCRIPTION FROM
(SELECT 
GLU.COMPANY,
GLU.ACCT_UNIT,
GLU.ACCOUNT,
AMV.MATRIX_CAT,
'+@COL+',
CASE 
WHEN @PERIOD=1 THEN GLU.DB_UNITS_01+GLU.CR_UNITS_01 
WHEN @PERIOD=2 THEN GLU.DB_UNITS_02+GLU.CR_UNITS_02 
WHEN @PERIOD=3 THEN GLU.DB_UNITS_03+GLU.CR_UNITS_03
WHEN @PERIOD=4 THEN GLU.DB_UNITS_04+GLU.CR_UNITS_04 
WHEN @PERIOD=5 THEN GLU.DB_UNITS_05+GLU.CR_UNITS_05 
WHEN @PERIOD=6 THEN GLU.DB_UNITS_06+GLU.CR_UNITS_06 
WHEN @PERIOD=7 THEN GLU.DB_UNITS_07+GLU.CR_UNITS_07 
WHEN @PERIOD=8 THEN GLU.DB_UNITS_08+GLU.CR_UNITS_08 
WHEN @PERIOD=9 THEN GLU.DB_UNITS_09+GLU.CR_UNITS_09
WHEN @PERIOD=10 THEN GLU.DB_UNITS_10+GLU.CR_UNITS_10
WHEN @PERIOD=11 THEN GLU.DB_UNITS_11+GLU.CR_UNITS_11 
WHEN @PERIOD=12 THEN GLU.DB_UNITS_12+GLU.CR_UNITS_12 
ELSE 0 END AS ACT
FROM GLUNITS GLU JOIN GLNAMES GLN 
ON GLU.COMPANY=GLN.COMPANY
AND GLU.ACCT_UNIT='''+'100'+'''
JOIN AUMXVALUE AMV
ON GLN.OBJ_ID=AMV.OBJ_ID
AND AMV.MATRIX_CAT='''+'_DIV'+'''
AND GLU.FISCAL_YEAR=@FISCAL_YEAR
AND GLU.ACCOUNT=9100
)UNITS
JOIN GLNAMES GLN
ON UNITS.COMPANY=GLN.COMPANY
AND UNITS.ACCT_UNIT=GLN.ACCT_UNIT) CURR
) T PIVOT( SUM(ACT) FOR MX_VALUE IN (' + @COL + ')) AS PIVTBL'


EXEC (@SQL)

由于