各位前辈我有两张表的数据(类似下表):现在要将他们合并插入到一张新表中去,按时间排序出来,如何编写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.   

    --假设新表为t3,且存在
    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 ...
      

  2.   

    不好意思说错了,是要得到下面的表,请我楼上的前辈,如何编写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

    ----需要得到如下的表
    时间       代号    表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 无
      

  3.   

    --查询如下:
    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.代号
      

  4.   

    ---测试数据---
    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 行受影响)
      

  5.   

    select 
      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 行受影响)**/
      

  6.   

    with tb as (
    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数量),'无')。