请问如何合并这些数据库记录?谢谢!

liuhong2003 2010-02-05 11:39:33
declare @t1 table ([customer_id] int
,[product_id] int
,[sale_type_id] int
,[price] decimal(10,4)
,[begin_date] datetime
,[end_date] datetime
,[price_key] int
,[price_source] int)
declare @t2 table ([customer_id] int
,[product_id] int
,[sale_type_id] int
,[price] decimal(10,4)
,[begin_date] datetime
,[end_date] datetime
,[price_key] int
,[price_source] int)
insert into @t1
select 1210001,1021,8,2.3000,'2009-12-01','2009-12-09',13835,3 union all
select 1210001,1021,8,2.1000,'2009-12-10','2009-12-30',14995,3 union all
select 1210001,1022,8,2.3000,'2009-12-01','2009-12-09',13836,3 union all
select 1210001,1022,8,2.1000,'2009-12-10','2009-12-30',14996,3 union all
select 1210001,1023,8,2.3000,'2009-12-01','2009-12-09',13837,3 union all
select 1210001,1023,8,2.1000,'2009-12-10','2009-12-30',14997,3 union all
select 1210001,1041,8,2.3000,'2009-12-01','2009-12-09',13838,3 union all
select 1210001,1041,8,2.0000,'2009-12-31','2009-12-31',15471,3 union all
select 1210001,1042,8,2.3000,'2009-12-01','2009-12-09',13839,3 union all
select 1210001,1042,8,2.0000,'2009-12-31','2009-12-31',15472,3 union all
select 1210001,1043,8,2.3000,'2009-12-01','2009-12-09',13840,3 union all
select 1210001,1043,8,2.0000,'2009-12-31','2009-12-31',15473,3 union all
select 1210001,1051,8,2.3000,'2009-12-01','2009-12-09',13841,3 union all
select 1210001,1051,8,2.3000,'2009-12-10','2009-12-30',15001,3 union all
select 1210001,1051,8,2.3000,'2009-12-31','2009-12-31',15474,3 union all
select 1210001,1052,8,2.3000,'2009-12-10','2009-12-30',15002,3 union all
select 1210001,1052,8,2.3000,'2009-12-31','2009-12-31',15475,3 union all
select 1210001,1053,8,2.3000,'2009-12-10','2009-12-20',15475,3
insert into @t2
select 1210001,1021,8,2.5440,'2009-12-01','2009-12-31',12033,1 union all
select 1210001,1022,8,2.5440,'2009-12-01','2009-12-31',12034,1 union all
select 1210001,1023,8,2.5440,'2009-12-01','2009-12-31',12035,1 union all
select 1210001,1041,8,2.8128,'2009-12-01','2009-12-31',12036,1 union all
select 1210001,1042,8,2.8128,'2009-12-01','2009-12-31',12037,1 union all
select 1210001,1043,8,2.8128,'2009-12-01','2009-12-31',12038,1 union all
select 1210001,1051,8,2.8128,'2009-12-01','2009-12-31',12039,1 union all
select 1210001,1052,8,2.8128,'2009-12-01','2009-12-31',12040,1
select * from @t1
select * from @t2
==============================
合并为大概如下:
select 1210001,1021,8,2.3000,'2009-12-01','2009-12-09',13835,3 union all
select 1210001,1021,8,2.1000,'2009-12-10','2009-12-30',14995,3 union all
select 1210001,1021,8,2.5440,'2009-12-31','2009-12-31',12033,1 union all
select 1210001,1041,8,2.3000,'2009-12-01','2009-12-09',13838,3 union all
select 1210001,1041,8,2.8128,'2009-12-10','2009-12-30',12036,1 union all
select 1210001,1041,8,2.0000,'2009-12-31','2009-12-31',15471,3

就是把@t1一个月少了的部分用@t2中的数据补上
谢谢!

...全文
65 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuhong2003 2010-02-05
  • 打赏
  • 举报
回复
@t1中最后一条应为:
select 1210001,1053,8,2.3000,'2009-12-10','2009-12-20',15476,3

@t2中少了一条,应加上
select 1210001,1053,8,2.8128,'2009-12-01','2009-12-31',12041,3
liuhong2003 2010-02-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 beirut 的回复:]
引用 2 楼 liuhong2003 的回复:
引用 1 楼 jane_64 的回复:
不明白
把@t1一个月少了的部分用@t2中的数据补上?都放到@t1中吗?


可以不用放到@t1中但要生成这部分数据:
select 1210001,1021,8,2.5440,'2009-12-31','2009-12-31',12033,1 union all
select 1210001,1041,8,2.8128,'2009-12-10','2009-12-30',12036,1

用日期范围判断是否少了?
[/Quote]

只生成一个月的单价
黄_瓜 2010-02-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liuhong2003 的回复:]
引用 1 楼 jane_64 的回复:
不明白
把@t1一个月少了的部分用@t2中的数据补上?都放到@t1中吗?


可以不用放到@t1中但要生成这部分数据:
select 1210001,1021,8,2.5440,'2009-12-31','2009-12-31',12033,1 union all
select 1210001,1041,8,2.8128,'2009-12-10','2009-12-30',12036,1
[/Quote]
用日期范围判断是否少了?
liuhong2003 2010-02-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jane_64 的回复:]
不明白
把@t1一个月少了的部分用@t2中的数据补上?都放到@t1中吗?
[/Quote]

可以不用放到@t1中但要生成这部分数据:
select 1210001,1021,8,2.5440,'2009-12-31','2009-12-31',12033,1 union all
select 1210001,1041,8,2.8128,'2009-12-10','2009-12-30',12036,1
Jane_64 2010-02-05
  • 打赏
  • 举报
回复
不明白
把@t1一个月少了的部分用@t2中的数据补上?都放到@t1中吗?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧