如何将行旋转为列
在 MSSQL 中使用 PIVOT 的每个示例都表明人们使用它来聚合数据。我正在尝试利用它来简单地将行转置为列
例如,考虑以下数据
SELECT 11826 ID,cast('64 ' as varchar(1000)) as answer,75098 QuestionID,2785 CollectionID into #temp
insert into #temp SELECT 11827 ID,cast('Security ' as varchar(1000)) as answer,75110 QuestionID,2785 CollectionID
insert into #temp SELECT 11828 ID,cast('42 ' as varchar(1000)) as answer,75115 QuestionID,2785 CollectionID
insert into #temp SELECT 11829 ID,cast('3/23/2010 12:01:00 AM ' as varchar(1000)) as answer,75119 QuestionID,2785 CollectionID
insert into #temp SELECT 11830 ID,cast('3/25/2010 ' as varchar(1000)) as answer,75120 QuestionID,2785 CollectionID
insert into #temp SELECT 11898 ID,cast('67 ' as varchar(1000)) as answer,75313 QuestionID,2792 CollectionID
insert into #temp SELECT 11899 ID,cast('True ' as varchar(1000)) as answer,75314 QuestionID,2792 CollectionID
insert into #temp SELECT 11900 ID,cast('0 ' as varchar(1000)) as answer,75315 QuestionID,2792 CollectionID
insert into #temp SELECT 11901 ID,cast('[email protected] ' as varchar(1000)) as answer,75316 QuestionID,2792 CollectionID
结果应该会产生类似于
CollectionID [AnswerFor75098] [AnswerFor75110] [AnswerFor75115] [AnswerFor75315]...
2785 64 Security 42
2792 Null Null Null 67
我一直在尝试 PIVOT 的结果,但我不确定这是正确的解决方案。如果是这样,有人有我可以使用的提示吗? 我想我可以在存储过程中公平地做到这一点,但是,如果可能的话,我试图避免使用游标。
感谢您的帮助
Every example of using PIVOT in MSSQL shows people using this to aggregate data. I'm trying to exploit this to just simply transpose rows to columns
For instance, consider the follwoing data
SELECT 11826 ID,cast('64 ' as varchar(1000)) as answer,75098 QuestionID,2785 CollectionID into #temp
insert into #temp SELECT 11827 ID,cast('Security ' as varchar(1000)) as answer,75110 QuestionID,2785 CollectionID
insert into #temp SELECT 11828 ID,cast('42 ' as varchar(1000)) as answer,75115 QuestionID,2785 CollectionID
insert into #temp SELECT 11829 ID,cast('3/23/2010 12:01:00 AM ' as varchar(1000)) as answer,75119 QuestionID,2785 CollectionID
insert into #temp SELECT 11830 ID,cast('3/25/2010 ' as varchar(1000)) as answer,75120 QuestionID,2785 CollectionID
insert into #temp SELECT 11898 ID,cast('67 ' as varchar(1000)) as answer,75313 QuestionID,2792 CollectionID
insert into #temp SELECT 11899 ID,cast('True ' as varchar(1000)) as answer,75314 QuestionID,2792 CollectionID
insert into #temp SELECT 11900 ID,cast('0 ' as varchar(1000)) as answer,75315 QuestionID,2792 CollectionID
insert into #temp SELECT 11901 ID,cast('[email protected] ' as varchar(1000)) as answer,75316 QuestionID,2792 CollectionID
The results should yield something like
CollectionID [AnswerFor75098] [AnswerFor75110] [AnswerFor75115] [AnswerFor75315]...
2785 64 Security 42
2792 Null Null Null 67
I've been experimenting with PIVOT however i'm not sure this is the correct solution. If so, does anybody have a hint i could use?
I think i can probably do this in a stored procedure fairly however, i'm trying to avoid using cursors if possible.
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找的是交叉表查询。如果您提前知道要查找的问题编号,则可以这样做:
但是,如果您想要的是根据问题动态确定列,那么您想要的是动态交叉表,而这在 SQL 中无法完成服务器没有一些模糊的动态 SQL。相反,您应该在中间层或报告工具中执行此操作。
What you are seeking is a crosstab query. If you know the question numbers you seek ahead of time, you can do it like so:
However, if what you want is to dynamically determine the columns based on questions, then what you want is a dynamic crosstab and that cannot be done in SQL Server without some fugly dynamic SQL. Instead, you should do this in the middle-tier or in a reporting tool.
人们在尝试 PIVOT 时通常没有意识到的一件事是,您需要了解您的列并将它们硬编码到查询中。这让我们思考:“这真的是最好的方法吗?”
即使您使用 Microsoft 的
PIVOT
语法,您也无法编写根据数据值动态添加列的通用 SQL 查询。PIVOT
仍然比编写一堆JOIN
更方便,所以我建议使用PIVOT
(如果您使用 Microsoft SQL服务器专用)。One thing that people often don't realize when they try
PIVOT
is that you need to know your columns and hard-code them into the query. This leads us to think, "is this really the best way?"You can't write a general-purpose SQL query that adds columns dynamically based on data values, even when you use Microsoft's
PIVOT
syntax.PIVOT
is still a bit more convenient than writing a bunch ofJOIN
s, so I'd suggest going withPIVOT
(if you use Microsoft SQL Server exclusively).我已经完成了一个快速 Sql 来展示如何将 Pivot 与示例数据一起使用。
我认为它可以改进以实现你想要的。
@community,请随时对此建议提出任何修改建议
I have done a quick Sql to show how to use Pivot with your sample data.
I think it can be improved to acheive what you want.
@community, please feel free to suggest any amendments to this suggestion