我需要将访问查询转换为sql服务器查询,但是出现错误,有人可以指出错误原因吗?
这是MS Access查询:
TRANSFORM Avg([X Avg Sub Group]) AS [AvgOfX Avg Sub Group]
SELECT Day, Process,
PARTNO_VAL0, CTQNO_VAL0,
ctq_description, MACHINE_VAL0,
usl, lsl,
Avg([X Avg Sub Group]) AS [Total Of X Avg Sub Group]
FROM [Capability Data with Shift]
WHERE (((Process)="BBB WELDING"))
GROUP BY Day, Process, PARTNO_VAL0, CTQNO_VAL0, ctq_description, MACHINE_VAL0, usl, lsl
PIVOT SHIFT_VAL0;
这是我所做的:
DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);
SELECT @cols = STUFF((SELECT distinct ',' + QUOTENAME(avg([X Avg Sub Group]))
FROM [Capability Data with Shift]
WHERE (((Process)='BBB WELDING'))
GROUP BY Day, Process, PARTNO_VAL0, CTQNO_VAL0, ctq_description, MACHINE_VAL0, usl, lsl, SHIFT_VAL0
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
SET @query = ' SELECT Day, Process, PARTNO_VAL0, CTQNO_VAL0, ctq_description, MACHINE_VAL0, usl, lsl, [Total Of X Avg Sub Group], ' + @cols + '
FROM
(
SELECT Day, Process, PARTNO_VAL0, CTQNO_VAL0, ctq_description, MACHINE_VAL0,
usl, lsl, SHIFT_VAL0, Avg([X Avg Sub Group]) AS [Total Of X Avg Sub Group]
FROM [Capability Data with Shift]
WHERE (((Process)=''BBB WELDING''))
GROUP BY Day, Process, PARTNO_VAL0, CTQNO_VAL0, ctq_description, MACHINE_VAL0, usl, lsl, SHIFT_VAL0
) t
PIVOT
(
avg([Total Of X Avg Sub Group])
FOR SHIFT_VAL0 IN( ' + @cols + ')
) p ';
Execute(@query);
这是我在SQL Server中执行查询时得到的结果:
(受影响的1行)
消息8114,级别16,状态1,行17转换数据类型时出错 nvarchar转换为int。
消息473,级别16,状态1,行17错误的值“ 0.000642857” 在PIVOT运算符中提供。
消息207,级别16,状态1,行1无效的列名称'X Total的平均值 子组”。
答案 0 :(得分:0)
我有用于旋转的通用存储过程
CREATE PROCEDURE [dbo].[msrTransformAsAccessDo]
@TRANSFORM_Function nvarchar(max),
@TRANSFORM_Field nvarchar(max),
@SQL_SELECT nvarchar(max),
@SQL_FROM_WHERE nvarchar(max),
@SQL_GROUPBY_HAVING_ORDERBY nvarchar(max),
@PIVOTBY nvarchar(max)
AS
BEGIN
SET NOCOUNT ON
DECLARE @DynSQL nvarchar(max)
SET @DynSQL = N' SELECT DISTINCT ' + @PIVOTBY + ' as key_value into ##pivoting ' + @SQL_FROM_WHERE + ' ORDER BY ' + @PIVOTBY
DROP TABLE IF EXISTS ##pivoting
EXEC (@DynSQL)
DECLARE @Values nvarchar(max) = N''
DECLARE @tmpStr nvarchar(max)
DECLARE @rsk CURSOR
SET @rsk = CURSOR SCROLL
FOR
select key_value from ##pivoting
OPEN @rsk
FETCH NEXT FROM @rsk INTO @tmpStr
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Values = @Values + ', ' + @TRANSFORM_Function + '(Case When convert(nvarchar,' + @PIVOTBY + ')=convert(nvarchar,''' + @tmpStr + ''') Then ' + @TRANSFORM_Field + ' Else Null End) AS [' + @tmpStr + '] '
FETCH NEXT FROM @rsk INTO @tmpStr
END
CLOSE @rsk
DROP TABLE IF EXISTS ##pivoting
SET @DynSQL = @SQL_SELECT + ' ' + @Values + ' ' + @SQL_FROM_WHERE + ' ' + @SQL_GROUPBY_HAVING_ORDERBY
EXEC (@DynSQL)
END
GO
并致电您的案例:
EXECUTE msrTransformAsAccessDo
'Avg',
'[X Avg Sub Group]',
'SELECT Day, Process, PARTNO_VAL0, CTQNO_VAL0, ctq_description, MACHINE_VAL0, usl, lsl, Avg([X Avg Sub Group]) AS [Total Of X Avg Sub Group] ',
'FROM [Capability Data with Shift] WHERE (((Process)=''BBB WELDING'')) ',
'GROUP BY Day, Process, PARTNO_VAL0, CTQNO_VAL0, ctq_description, MACHINE_VAL0, usl, lsl ',
'SHIFT_VAL0'