帮助!无法得到所需的结果
使用 SQL Server 2008(R2)
我有一个需要以不同方式显示的结果集,它当前可用,如下所示:
Resource Nr Cost StartDate EndDate
Cable_5m 8 3.5 12/03/2011 13/03/2011
Cable_5m 2 3.5 13/03/2011 14/03/2011
Rope125A 1 0 16/03/2011 18/03/2011
Rope125A 1 0 17/03/2011 17/03/2011
我需要的是按日期报告发送的每种资源的数量,如下所示:
DateOfEvnt Resource Nr Cost
2011-03-12 Cable_5m 8 3.50
2011-03-13 Cable_5m 10 3.50
2011-03-14 Cable_5m 2 3.50
2011-03-16 Rope125A 1 0.00
2011-03-17 Rope125A 2 0.00
2011-03-18 Rope125A 1 0.00
我有看过枢轴,但我一生都无法让它发挥作用,而且我不确定枢轴是否是我所需要的。所有帮助将不胜感激。
Using SQL Server 2008(R2)
I have a result set that I need displaying in a different way, it is currently available like:
Resource Nr Cost StartDate EndDate
Cable_5m 8 3.5 12/03/2011 13/03/2011
Cable_5m 2 3.5 13/03/2011 14/03/2011
Rope125A 1 0 16/03/2011 18/03/2011
Rope125A 1 0 17/03/2011 17/03/2011
What I need is to report on a date basis how many of each resource are sent, like below:
DateOfEvnt Resource Nr Cost
2011-03-12 Cable_5m 8 3.50
2011-03-13 Cable_5m 10 3.50
2011-03-14 Cable_5m 2 3.50
2011-03-16 Rope125A 1 0.00
2011-03-17 Rope125A 2 0.00
2011-03-18 Rope125A 1 0.00
I have looked at pivot but cannot for the life of me get it to work, and I am not sure pivot is what I need. All help would be gratefully recieved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于给定的输入和输出,您根本不需要
PIVOT
。一个简单的UNION
和GROUP BY
就足够了。测试数据
SQL语句
编辑
虽然
UNION
和GROUP BY
满足结果,但我开始怀疑您更需要这样的东西,其中WITH
语句返回Start-
和EndDate
之间每一天的记录。For given in and outputs, you don't need a
PIVOT
at all. A simpleUNION
andGROUP BY
would suffice.Test data
SQL Statement
EDIT
While the
UNION
andGROUP BY
satisfy the results, I'm starting to suspect you are more in need of something like this where theWITH
statement returns a record for each day betweenStart-
andEndDate
.