Sql Server 2005 数据透视表可以将 nText 传递到其中吗?
一个简单的问题是我可以将 nText 输入到数据透视表中吗? (SQL Server 2005)
我有一个表,记录了调查问卷的答案,其中包含以下元素,例如:
UserID QuestionNumber Answer
Mic 1 Yes
Mic 2 No
Mic 3 Yes
Ste 1 Yes
Ste 2 No
Ste 3 Yes
Bob 1 Yes
Bob 2 No
Bob 3 Yes
答案保存在 nText 中。无论如何,像数据透视表一样的 id 要做的是:
UserID 1 2 3
Mic Yes No Yes
Ste Yes No Yes
Bob Yes No Yes
我有一些测试代码,它创建一个数据透视表,但目前它只显示每列中的答案数量(代码可以在下面找到)。所以我只想知道是否可以将 nText 添加到数据透视表?当我尝试时它会出现错误,并且有人在另一个网站上指出这是不可能的,所以我想检查是否是这种情况。
仅供进一步参考,我没有机会更改数据库,因为它链接到我尚未创建或也无法访问的其他系统。
下面是我目前拥有的 SQL 代码:
DECLARE @query NVARCHAR(4000)
DECLARE @count INT
DECLARE @concatcolumns NVARCHAR(4000)
SET @count = 1
SET @concatcolumns = ''
WHILE (@count <=52)
BEGIN
IF @COUNT > 1 AND @COUNT <=52
SET @concatcolumns = (@concatcolumns + ' + ')
SET @concatcolumns = (@concatcolumns + 'CAST ([' + CAST(@count AS NVARCHAR) + '] AS NVARCHAR)')
SET @count = (@count+1)
END
DECLARE @columns NVARCHAR(4000)
SET @count = 1
SET @columns = ''
WHILE (@count <=52)
BEGIN
IF @COUNT > 1 AND @COUNT <=52
SET @columns = (@columns + ',')
SET @columns = (@columns + '[' + CAST(@count AS NVARCHAR) + '] ')
SET @count = (@count+1)
END
SET @query = '
SELECT UserID,
' + @concatcolumns + '
FROM(
SELECT
UserID,
QuestionNumber AS qNum
from QuestionnaireAnswers
where QuestionnaireID = 7
) AS t
PIVOT
(
COUNT (qNum)
FOR qNum IN (' + @columns + ')
) AS PivotTable'
select @query
exec(@query)
Right bit of a simple question can I input nText into a pivot table? (SQL Server 2005)
What I have is a table which records the answers to a questionnaire consisting of the following elements for example:
UserID QuestionNumber Answer
Mic 1 Yes
Mic 2 No
Mic 3 Yes
Ste 1 Yes
Ste 2 No
Ste 3 Yes
Bob 1 Yes
Bob 2 No
Bob 3 Yes
With the answers being held in nText. Anyway what id like a Pivot table to do is:
UserID 1 2 3
Mic Yes No Yes
Ste Yes No Yes
Bob Yes No Yes
I have some test code, that creates a pivot table but at the moment it just shows the number of answers in each column (code can be found below). So I just want to know is it possible to add nText to a pivot table? As when I've tried it brings up errors and someone stated on another site that it wasn't possible, so I would like to check if this is the case or not.
Just for further reference I don't have the opportunity to change the database as it's linked to other systems that I haven't created or have access too.
Heres the SQL code I have at present below:
DECLARE @query NVARCHAR(4000)
DECLARE @count INT
DECLARE @concatcolumns NVARCHAR(4000)
SET @count = 1
SET @concatcolumns = ''
WHILE (@count <=52)
BEGIN
IF @COUNT > 1 AND @COUNT <=52
SET @concatcolumns = (@concatcolumns + ' + ')
SET @concatcolumns = (@concatcolumns + 'CAST ([' + CAST(@count AS NVARCHAR) + '] AS NVARCHAR)')
SET @count = (@count+1)
END
DECLARE @columns NVARCHAR(4000)
SET @count = 1
SET @columns = ''
WHILE (@count <=52)
BEGIN
IF @COUNT > 1 AND @COUNT <=52
SET @columns = (@columns + ',')
SET @columns = (@columns + '[' + CAST(@count AS NVARCHAR) + '] ')
SET @count = (@count+1)
END
SET @query = '
SELECT UserID,
' + @concatcolumns + '
FROM(
SELECT
UserID,
QuestionNumber AS qNum
from QuestionnaireAnswers
where QuestionnaireID = 7
) AS t
PIVOT
(
COUNT (qNum)
FOR qNum IN (' + @columns + ')
) AS PivotTable'
select @query
exec(@query)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将其转换为
nvarchar(MAX)
- 就可以正常工作了。仅供参考,您实际上不应该在 2005+ 下使用ntext
- 它已被弃用。希望有帮助。Just convert it to an
nvarchar(MAX)
- that will work just fine. FYI, you shouldn't really be usingntext
under 2005+ - it's deprecated. Hope that helps.不要使用 while 循环来创建列名称,而是使用函数
COALESCE
。您还可以使用函数QuoteName
将列名称放在方括号中。Instead of using a while loop making column names, use the function
COALESCE
. And also you can use the functionQuoteName
to make the column name in square brackets.