SQL Server 2005 如何透视或交叉表数据来获取结果集?
我希望生成一个存储过程,它将采用 projectIds 的 CSV 字符串并返回以下结果集。
projectId 和 name 字段来自项目表,而其余字段是每个项目的里程碑日期的关键。里程碑字段适用于 (37, 39, 41, 45, 47, 50, 53, 72, 73, 75, 77) 中的某些 CID 里程碑,我希望每个相应的里程碑在 excel 文件中显示为名称(I我猜测为每个里程碑使用别名将每个里程碑名称转换为我希望它显示的内容)
另请注意,第一个里程碑字段是 BRS (cid=37) 开始日期,其余里程碑字段都是结束日期,包括里程碑 cid 37 和上面提到的其余部分。
日期应代表具有可用数据的项目里程碑日期,如果特定 CID 没有项目里程碑,那么我需要使用 ReleaseSchedule 日期。我打算通过 COALESCE(项目里程碑日期、发布计划日期)来完成此任务。
里程碑名称的字段标题如下:
CID NAME in result set (as field headers)
37 BRS
39 SRS
41 SAD
45 Product Profile Review
47 SE Integration
50 IDE
53 UAT
72 PE Testing
73 Code Freeze
75 Dark Pod
77 Production
I am looking to generate a stored proc that will take a CSV string of projectIds and return the following result set.
the projectId and name fields come from the project table while the rest of the fields are a pivot of the milestone dates for each project. the milestone fields are for certain milestones of CID in (37, 39, 41, 45, 47, 50, 53, 72, 73, 75, 77) where I want each respective milestone to show as the name in the excel file (I am guessing to use an alias for each to convert each milestone name to what I want it to show as)
Also note the first milestone field is the BRS (cid=37) start date and the rest of the milestone fields are all end dates including milestone cid 37 and the rest mentioned above.
The dates should represent the projectMilestone dates where available data is had, if there is no projectMilestone for a particular CID then i need to use the ReleaseSchedule date. i was going to COALESCE(projectmilestone dates, releaseschedual dates) to accomplish this.
the field headers of the milestone names would be such:
CID NAME in result set (as field headers)
37 BRS
39 SRS
41 SAD
45 Product Profile Review
47 SE Integration
50 IDE
53 UAT
72 PE Testing
73 Code Freeze
75 Dark Pod
77 Production
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@beth,我现在可以使用下面的代码为单个项目tId 进行此工作(某种程度上)。但我没有使用透视,而是手动使用 case 语句来在最后旋转数据:
@beth i have this working (sort of) now for individual projecttId's with the below code. but instead of using pivot i have manual case statements to pivot the data at the end:
不确定您是否可以完全自动化您想要的一切。
回复:我希望每个里程碑在 Excel 文件中显示为名称(我猜测为每个里程碑使用别名将每个里程碑名称转换为我希望它显示的名称)
你可以使用查找表?
回复:另请注意,第一个里程碑字段是 BRS (cid=37) 开始日期,其余里程碑字段都是结束日期
在数据透视之前,这些字段必须位于等效输出中柱子。在数据透视期间,您必须对值应用聚合函数,例如first()、min() 或max()。
回复:日期应代表具有可用数据的项目里程碑日期,如果特定 CID 没有项目里程碑,那么我需要使用 ReleaseSchedule 日期。
再次,您需要执行此工作在枢轴之前。
您能否以您想要的方式获得未透视的结果集,将项目与projectMilestone 和releaseSchedule 结合(仅适用于projectMilestone 中的间隙)并查找您想要显示的列标题名称?
Not sure if you can fully automate everything you want.
re: I want each respective milestone to show as the name in the excel file (I am guessing to use an alias for each to convert each milestone name to what I want it to show as)
can you use a lookup table?
re: Also note the first milestone field is the BRS (cid=37) start date and the rest of the milestone fields are all end dates
Before the pivot, those fields will have to be in an equivalent output column. During the pivot you have to apply an aggregate function to the values, like first(), min(), or max().
re: The dates should represent the projectMilestone dates where available data is had, if there is no projectMilestone for a particular CID then i need to use the ReleaseSchedule date.
again, you'll need to do this work before the pivot.
Can you get the unpivoted result set the way you want joining project with projectMilestone and releaseSchedule (for the gaps in projectMilestone only) and looking up your column header names you want displayed?