插入排除第二个,从现有记录中添加第一个
我想找到比我的解决方案更有效的方法。所以问题是这样的: 我想从 select 语句进行批量插入。 (背景信息:我正在使用 MSSQL 2005)
选择示例:
SELECT number, amount, year, modifiedDate, itm, city, c, d, e, f.... FROM X
JOIN Y ....
因此,如果结果是
...
Num Amount Year ModifiedDate Itm City ... ...
1 100 2011 01-01-2011 2 Amsterdam .. ..
1 100 2011 01-02-2011 5 Den Haag .. ..
2 4560 2011 01-02-2011 6 Amsterdam .. ..
33 456 2010 01-02-2011 12 Leiden .. ..
22 456 2010 01-02-2011 12 Leiden .. ..
....
在我想要的目标中:
...
Num Amount Year ModifiedDate Itm City ... ...
1 100 2011 01-02-2011 5 Den Haag .. ..
2 4560 2011 01-02-2011 6 Amsterdam .. ..
33 456 2010 01-02-2011 12 Leiden .. ..
22 456 2010 01-02-2011 12 Leiden .. ..
....
没有此记录: ( 1 100 2011 01-01-2011 2 阿姆斯特丹)
我想插入第一个来自目标表中的这两个。我还有其他来自选择的记录也应该插入。因此,在这种情况下,我想采用第一个,其中数字、金额和年份按 ModifiedDate DESC 顺序相同。这就是我想做的。我已经使用 Cursor 做了一个解决方案,但应该有更好的方法。
I want to find more efficient way than my solution. So here is the problem:
I want to have a bulk insert from a select statement. (Background information: I am using MSSQL 2005)
Example of the select:
SELECT number, amount, year, modifiedDate, itm, city, c, d, e, f.... FROM X
JOIN Y ....
So if the result is
...
Num Amount Year ModifiedDate Itm City ... ...
1 100 2011 01-01-2011 2 Amsterdam .. ..
1 100 2011 01-02-2011 5 Den Haag .. ..
2 4560 2011 01-02-2011 6 Amsterdam .. ..
33 456 2010 01-02-2011 12 Leiden .. ..
22 456 2010 01-02-2011 12 Leiden .. ..
....
In the destination I want to have:
...
Num Amount Year ModifiedDate Itm City ... ...
1 100 2011 01-02-2011 5 Den Haag .. ..
2 4560 2011 01-02-2011 6 Amsterdam .. ..
33 456 2010 01-02-2011 12 Leiden .. ..
22 456 2010 01-02-2011 12 Leiden .. ..
....
Without this record: ( 1 100 2011 01-01-2011 2 Amsterdam)
I want to insert the first from these two in a destination table. I have other records coming out of the select that should be inserted as well. So in this case I want to take the first one where number, amount and year are the same order by modifiedDate DESC. That is what I want to do. I already made a solution using Cursor, but there should be a better way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢您的所有回复。我有一个不同的想法,它比老式光标快得多,而且我认为它符合我正在寻找的东西,所以我也在这里分享它。
Thank you for all the responses. I got a different idea which is way faster than the old-fashion cursor and I think it matches what I was looking for, so I am sharing it here as well.
这应该会让您更接近您所需要的:
This should get you closer to what you need:
如果我理解您想要正确执行的操作,我认为您需要对前三列进行分组,然后从
modifiedDate
列中选择MAX
值。如果我误解了,请告诉我。
如果您还在查询的
INSERT
部分寻求帮助,请提供有关您当前正在执行的操作的更多详细信息。If I understand what you're trying to do correctly, I think you need to group on the first three columns, and select the
MAX
value from themodifiedDate
column.Let me know if I have misunderstood.
If you're also looking for help on the
INSERT
part of the query, please give more details on what you're currently doing there.按所有列进行分组即可实现此目的。
例子:
Grouping by all your columns will acheive this.
Example: