各位前辈我有两张表的数据(类似下表):现在要将他们合并插入到一张新表中去,按时间排序出来,如何编写SQL语句?
----------表1
时间 代号 数量
20101201 a 100
20101203 a 50
20101204 b 20
20101205 c 50
20101207 d 80
20101208 e 70
20101210 b 140
20101213 f 130-----表2
时间 代号 数量
20101128 e 50
20101202 d 200
20101205 a 50
20101208 b 20
20101211 c 50
20101212 f 20----需要得到如下的表时间 代号 数量
20101128 e 50
20101201 a 100
20101202 d 200
20101203 a 50
20101204 b 20
20101205 a 50
20101206 c 50
20101207 d 80
20101208 b 20
20101209 e 70
20101210 b 140
20101211 c 50
20101212 f 20
20101213 f 130
----------表1
时间 代号 数量
20101201 a 100
20101203 a 50
20101204 b 20
20101205 c 50
20101207 d 80
20101208 e 70
20101210 b 140
20101213 f 130-----表2
时间 代号 数量
20101128 e 50
20101202 d 200
20101205 a 50
20101208 b 20
20101211 c 50
20101212 f 20----需要得到如下的表时间 代号 数量
20101128 e 50
20101201 a 100
20101202 d 200
20101203 a 50
20101204 b 20
20101205 a 50
20101206 c 50
20101207 d 80
20101208 b 20
20101209 e 70
20101210 b 140
20101211 c 50
20101212 f 20
20101213 f 130
解决方案 »
- 统计2万条数据中的100种条件分别由多少条,竟然用了8秒怎么办
- sqlserver复制时会造成数据查询死锁 请邹老大帮忙
- 一个SQL的效率 求解答
- 为什么我得不到正确的记录总数呢Select @Total=Count(id) From Js
- 求助啊!哪位大神能帮帮我,我们要考试了,可是我完全混过来的,期末题不给答案。。。求帮助!!!
- 如何用最简单的办法将sqlserver中的某个表的表结构连同表里面的数据保存起来存储到其他数据库中?
- 感谢、感谢大家,感谢老大
- 急!存储过程问题
- 请问 Delphi5.0中的ADO 在SQL server 2000 下报错! ?
- 在一台电脑上能不能同时安装oracle server 和oracle client
- sql 两个表查询问题,请教
- 拜求BOM表原材料展开递归查询
insert into t3 select * from t1 union all select * from t2
select * from t3 order by ...--如果表t3不存在
select * into t3 from
(select * from t1 union all select * from t2) t
select * from t3 order by ...
----------表1
时间 代号 数量
20101201 a 100
20101203 a 50
20101204 b 20
20101205 c 50
20101207 d 80
20101208 e 70
20101210 b 140
20101213 f 130-----表2
时间 代号 数量
20101128 e 50
20101202 d 200
20101205 a 50
20101208 b 20
20101211 c 50
20101212 f 20
----需要得到如下的表时间 代号 表1数量 表2数量
20101128 e 无 50
20101201 a 100 无
20101202 d 无 200
20101203 a 50 无
20101204 b 20 无
20101205 a 无 50
20101206 c 50 无
20101207 d 80 无
20101208 b 无 20
20101209 e 70 无
20101210 b 140 无
20101211 c 无 50
20101212 f 无 20
20101213 f 130 无
select isnull(m.时间,n.时间) 时间 , isnull(m.代号,n.代号) 代号 , isnull(ltrim(m.数量),'无') 表1数量,isnull(ltrim(n.数量),'无') 表2数量
from tb1 m full join tb2 n on m.时间 = n.时间 and m.代号 = n.代号--如果表3存在如下
insert into t3
select isnull(m.时间,n.时间) 时间 , isnull(m.代号,n.代号) 代号 , isnull(ltrim(m.数量),'无') 表1数量,isnull(ltrim(n.数量),'无') 表2数量
from tb1 m full join tb2 n on m.时间 = n.时间 and m.代号 = n.代号--如果表3不存在,如下:
select isnull(m.时间,n.时间) 时间 , isnull(m.代号,n.代号) 代号 , isnull(ltrim(m.数量),'无') 表1数量,isnull(ltrim(n.数量),'无') 表2数量
into t3
from tb1 m full join tb2 n on m.时间 = n.时间 and m.代号 = n.代号
if object_id('[t1]') is not null drop table [t1]
go
create table [t1]([时间] datetime,[代号] varchar(1),[数量] int)
insert [t1]
select '20101201','a',100 union all
select '20101203','a',50 union all
select '20101204','b',20 union all
select '20101205','c',50 union all
select '20101207','d',80 union all
select '20101208','e',70 union all
select '20101210','b',140 union all
select '20101213','f',130
go
if object_id('[t2]') is not null drop table [t2]
go
create table [t2]([时间] datetime,[代号] varchar(1),[数量] int)
insert [t2]
select '20101128','e',50 union all
select '20101202','d',200 union all
select '20101205','a',50 union all
select '20101208','b',20 union all
select '20101211','c',50 union all
select '20101212','f',20
go---查询---
select
convert(varchar(10),isnull(t1.时间,t2.时间),120) as 时间,
isnull(t1.代号,t2.代号) as 代号,
isnull(ltrim(t1.数量),'无') as 表1数量,
isnull(ltrim(t2.数量),'无') as 表2数量
from t1
full join t2 on t1.时间=t2.时间 and t1.代号=t2.代号
order by 1,2---结果---
时间 代号 表1数量 表2数量
---------- ---- ------------ ------------
2010-11-28 e 无 50
2010-12-01 a 100 无
2010-12-02 d 无 200
2010-12-03 a 50 无
2010-12-04 b 20 无
2010-12-05 a 无 50
2010-12-05 c 50 无
2010-12-07 d 80 无
2010-12-08 b 无 20
2010-12-08 e 70 无
2010-12-10 b 140 无
2010-12-11 c 无 50
2010-12-12 f 无 20
2010-12-13 f 130 无(14 行受影响)
convert(varchar(8),isnull(t1.时间,t2.时间),112) as 时间,
isnull(t1.代号,t2.代号) as 代号,
isnull(ltrim(t1.数量),'无') as 表1数量,
isnull(ltrim(t2.数量),'无') as 表2数量
from t1
full join t2 on t1.时间=t2.时间 and t1.代号=t2.代号
order by 1,2
/**
时间 代号 表1数量 表2数量
-------- ---- ------------ ------------
20101128 e 无 50
20101201 a 100 无
20101202 d 无 200
20101203 a 50 无
20101204 b 20 无
20101205 a 无 50
20101205 c 50 无
20101207 d 80 无
20101208 b 无 20
20101208 e 70 无
20101210 b 140 无
20101211 c 无 50
20101212 f 无 20
20101213 f 130 无(14 行受影响)**/
select 时间,代号,数量 as 表1数量,NULL as 表2数量
from 表1
UNION ALL
select 时间,代号,NULL as 表1数量,数量 as 表2数量
from 表2
)
select 时间,代号,ISNULL(表1数量,'无') 表1数量,ISNULL(表2数量,'无') 表2数量
from tb--1、给的数据有问题,不了解最后结果的数据筛选方式
,比如20101205 a 50 和 20101205 c 50 是如何取舍。--2、可以参考上述方式实现,如果isnull提示转换错误,如下处理ISNULL(ltrim(表1数量),'无')。