SQL Server 2000数据透视查询
如果之前已经回答过这个问题,请原谅我。
我正在寻找有关如何构建数据透视表或交叉表查询的一些建议。
我有一个看起来像这样的表格,
Vessel Date Inspector
A 02/05/10 Morris
B 05/20/10 Clouseau
A 07/25/10 Gadget
我需要结果看起来像这样
Vessel Jan Feb Mar April May June July Aug Sept Oct Nov Dec
A Morris Gadget
B Clouseau
希望这是有道理的。我希望有人能给我一些建议或帮助我开始这方面的工作。
谢谢
Please forgive me if this has been answered before.
I'm looking for some advice on how to build a pivot or cross tab query.
I have a table that looks likes this
Vessel Date Inspector
A 02/05/10 Morris
B 05/20/10 Clouseau
A 07/25/10 Gadget
I need the results to be look like this
Vessel Jan Feb Mar April May June July Aug Sept Oct Nov Dec
A Morris Gadget
B Clouseau
Hopefully that makes sense. I'm hoping someone can give me some advice or help to get me started on this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须静态声明列。应该注意的是,这仅适用于一年。如果您正在寻找的是动态列(月)生成,那么尝试在 T-SQL 中执行此操作并不是正确的方法,因为您只能使用一些丑陋的动态 SQL 来执行此操作。相反,您应该使用报告生成器或中间层组件来构建结果集。
You have to statically declare the columns. It should be noted that this will only work for a single year. If what you are seeking is dynamic column (month) generation, then trying to do it in T-SQL is not the right approach as you can only do it with some fugly dynamic SQL. Instead, you should use a report generator or middle-tier component to build the result set.
我认为您不需要为此使用 PIVOT - 只需创建 12 个子查询,每个月一个:
您可以通过添加
AND YEAR(Date)=2010
(对于示例)到 12 个子查询中每个子查询的末尾。I don’t think you need a PIVOT for this — just create 12 subqueries, one for each month:
You can make this work for any particular year by adding a
AND YEAR(Date)=2010
(for example) to the end of each of the 12 subqueries.