我想把表Z1中的Rdt,P,Sid三个字段和表Q0Z0中的Rdt,P,Sid三个字段查询出来,然后存入到表KC_P(Rdt,P,Kid)中。
但是由于表Q0Z0中的Sid和Z1表中的Sid的值存在重复的情况,所以我想在查询的时候给Q0Z0表中的Sid字段加上100,以避免在KC_P表中出现重复的Kid号。下面是我写的句子,运行不正确,请高手给指点一下,或者给出更好的句子,谢谢!
insert KC_P(Rdt,P,Kid)
select *
from 

select Rdt,P,Sid
from Z1 
where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
group by Rdt 
) A join ( 
select Rdt,Pt,Sid+100
from Q0Z0 
where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
group by Rdt 
) B 
on A.Rdt=B.Rdt 

解决方案 »

  1.   

    补充一下查询条件:就是把Z1表和Q0Z0表中的Rdt日期字段的分钟部分为0或30的所有记录查询出来。即整点和半小时的点。
      

  2.   

    insert KC_P(Rdt,P,Kid)
    select * --貌似楼主的语法也错了吧,这里有几列?
    from 

    select Rdt,P,Sid
    from Z1 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    group by Rdt 
    ) A join ( 
    select Rdt,Pt,Sid+100
    from Q0Z0 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    group by Rdt 
    ) B 
    on A.Rdt=B.Rdt 表Q0Z0中的Rdt,P,Sid三个字段查询出来,然后存入到表KC_P(Rdt,P,Kid)中怎么个存法????
      

  3.   

    insert KC_P(Rdt,P,Kid)
    select *
    from 
    (
    select *
    from 

    select Rdt,P,Sid
    from Z1 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    group by Rdt 
    ) A 
    union all
    select * 
    from 

    select Rdt,Pt,Sid+100
    from Q0Z0 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    group by Rdt 
    ) B 
    ) j
      

  4.   

    insert KC_P(Rdt,P,Kid)
    select *
    from 

    select Rdt,P,Sid
    from Z1 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    group by Rdt 
    ) A 
    union all
    select * 
    from 

    select Rdt,Pt,Sid+100
    from Q0Z0 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    group by Rdt 
    ) B 
    这样好了
      

  5.   

    insert KC_P(Rdt,P,Kid)
    select RDT,SID
    from 

    select Rdt,P,Sid
    from Z1 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    group by Rdt 
    ) A 
    union all
    select RDT,SID
    from 

    select Rdt,Pt,Sid+100  AS SID
    from Q0Z0 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    group by Rdt 
    ) B 
      

  6.   

    insert KC_P(Rdt,P,Kid)
    select *
    from 

    select Rdt,P,Sid
    from Z1 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    group by Rdt 
    ) A 
    union all
    select * 
    from 

    select Rdt,Pt,Sid+100  AS SID
    from Q0Z0 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    group by Rdt 
    ) B 这样就行了,不好意思,只看楼主的题目,语句列数看错了,
      

  7.   

    就是三个字段啊,表KC_P就三个字段,我就是想把另两个表中的相应三个字段的记录查询出来,然后存入到KC_P中。
      

  8.   

    insert KC_P(Rdt,P,Kid)
    select *
    from 

    select Rdt,P,Sid
    from Z1 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    --group by Rdt 
    ) A 
    union all
    select * 
    from 

    select Rdt,Pt,Sid+100  AS SID
    from Q0Z0 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    --group by Rdt 
    ) B 楼主GROUP BY 是要如何?要不然直接DISTINCT
      

  9.   


    insert KC_P(Rdt,P,Kid)
    select *
    from 

    select Rdt,P,Sid
    from Z1 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    ) A 
    union all
    select * 
    from 

    select Rdt,Pt,Sid+100 as sid
    from Q0Z0 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'
    ) B 
      

  10.   


    能运行成功么?如果能成功 但是慢的话 
    问题可能出在这里where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'有索引么 
      

  11.   

    时间字段加索引试试吧insert KC_P(Rdt,P,Kid)
    select Rdt,P,Sid
    from Z1 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'unionselect Rdt,Pt,Sid+100 as sid
    from Q0Z0 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'语句还可以简一下
      

  12.   

    其实我已经加了索引,不过是针对Rdt和Sid建的复合聚集索引。是不是有影响速度啊?
      

  13.   


    没有包含P吗,?
    还有RDT是索引列的第列还是?SQL 是0几的?
    大于7.1号的占总数据量的多少,
    还有KC-P表里的索引情况,数据情况?
      

  14.   

    能运行成功,Z1和Q0Z0表中有针对Rdt和Sid字段建的复合聚集索引,刚才运行了15分钟还没结果,我就强制停止运行了,但是表KC_P中没有插入任何数据。
      

  15.   

    ..那个列放在索引的前面定义的 (Rdt,sid) or (sid,rdt)
      

  16.   

    索引都没有包含P,都只是给时间字段和Sid字段加索引了,KC_P里面只对时间字段加聚集索引了,表中大于7月1号的数据占总数据量的很小一部分,数据是从3月1号开始的,而7月2号以后就已经没有数据了。另外Rdt是递增的索引。
      

  17.   


    索引是(sid,rdt)顺序的。是不是该把时间字段放前面啊?应该怎么设计这个索引,才能最大效率的执行查询呢?
      

  18.   

    1建立一个包含P的非聚集索引,它会自动包含聚集索引的两列,
    插入表数据时,2ORDER BY RDT 排好序这样应该差不多了,
      

  19.   

    这个不能只针对这一个语句,要针对数据库的大部分语句来,针对这个是时间放前面,因为这样才有统计信息
    看来2000只能这样建立个非聚集索引
    CREATE INDEX IDX_RDT_SID_P ON TB(RDT,SID,P)
    insert KC_P(Rdt,P,Kid)
    SELECT * FROM (
    select Rdt,P,Sid
    from Z1 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1'unionselect Rdt,Pt,Sid+100 as sid
    from Q0Z0 
    where datepart(mi,Rdt) in (0,30) and Rdt>'2010-7-1') T ORDER BY RDT插入的数据如果占,KC-P的绝大多数,就把聚集索引弄掉再重建吧,禁用非聚集索引