复制上个月的值并插入到新行中
这是我当前表的一个示例:
1)表名称:TotalSales
Name Year Month Sales
------ ---- ----- -----
Alfred 2011 1 100
我想要做的是创建一个像这样的表,添加一个新行(上个月销售):
2)表名称:TotalSales
Name Year Month Sales Prior month sales
------ ---- ----- ----- -----------------
Alfred 2011 2 110 100
不知道如何做,但这就是我一直在努力的事情:
SELECT Name, Year, Month, Sales, Sales as [Prior Month sales]
FROM TotalSales
WHERE
DATEPART(month, [Prior Month sales]) = DATEPART(month, DATEADD(month, -1, getdate()))
感谢您的帮助
Here is an example of the current table I have:
1) Table name: TotalSales
Name Year Month Sales
------ ---- ----- -----
Alfred 2011 1 100
What I want to do is create a table like this, add a new row(Prior month sales):
2) Table name: TotalSales
Name Year Month Sales Prior month sales
------ ---- ----- ----- -----------------
Alfred 2011 2 110 100
Not sure how to this, but this is what I have been working on:
SELECT Name, Year, Month, Sales, Sales as [Prior Month sales]
FROM TotalSales
WHERE
DATEPART(month, [Prior Month sales]) = DATEPART(month, DATEADD(month, -1, getdate()))
Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信这应该有效...您需要在名称/上个月加入自身,但是由于年/月是单独存储的,所以您有上个月的 2 个测试用例。
I believe this should work...you need to join to itself on name/prior month, but you have 2 test cases for prior month since year/month are stored separately.
要选择给定的数据,您需要将表连接到自身:
LEFT OUTER JOIN
是一个外部连接,以防他们上个月没有任何销售额(或者这是他们的第一个月)公司)。To select the given data you need to join the table to itself:
The
LEFT OUTER JOIN
is an outer join in case they didn't have any sales the previous month (or this is their first month with the company).尝试这样的方法,用您想要的值更新表......
Try something like this to just update the table with the values you want...