表1:
槽号  品味       日期
101   al99.90    2005-1-30
表2:
品味   一车间   二车间    日期
其中一车间的槽号是以1,2,3开头,二车间的槽号是4,5,6开头
品味为7种,是固定的,如果表一中没有,则表二中显示为0。
如:表一中没有品味为al99.00则:
表二:
al99.00   0      0     2005-1-30
品味:al99.90,al99.85,al99.70A,AL99.70,AL99.60 ,AL99.50,AL99.00
从表一到表二怎么做效率高点?

解决方案 »

  1.   

    with adoquery1 do
    begin
      close;
      sql.text:='select count(*) from 表2';
      open;
      if fields[0].asinteger >0 then
      begin
        close;
        sql.text:='delete 表2'
        ExecSQL;
      end;
      close;
      sql.text:='insert into 表2 select 品味,count(品味),0,日期  from 表1 where 
        left(rtrim(品味),1) in ('1','2','3') group by 品位,日期'; 
      execSQL;
      close;
      SQL.text:='update 表2 set 二车间=(select count(*) from 表1 where left(rtrim(品味),1) 
        in ('4','5','6') and 日期=表2.日期 and 品位=表2.品味 group by 品味,日期 ) '
      ExecSQL;
    end;
     随手写的 可能不对
      

  2.   

    create table tab1
    (槽号 char(3),
     品味 char(10),
     日期 datetime
     )insert into tab1
    select 
    '101','al99.90', '2005-1-30'
    select 品味, 
    车间1=max(case when left(槽号,1) in('1','2','3') then 槽号 else 0 end),
    车间2=max(case when left(槽号,1) in('4','5','6') then 槽号 else 0 end),
    日期
    from tab1
    group by 品味,日期
    drop table tab1
      

  3.   

    你是什么数据库??
    SQLserver?oracle?还是access?
      

  4.   

    要求分开,是统计个数。
    如:
    表1:
    槽号  品味       日期
    101   al99.90    2005-1-30
    102   al99.90    2005-1-30
    表2:
    品味   一车间   二车间    日期
    al99.90   2       0       2005-1-30
    而表二每天都产生:
    品味   一车间   二车间    日期
    al99.90   2       0       2005-1-30
    al99.85   2       0       2005-1-30
    al99.70a  2       0       2005-1-30
    al99.70   2       0       2005-1-30
    al99.60   2       0       2005-1-30
    al99.50   2       0       2005-1-30
    al99.00   0      0       2005-1-30只是后面的数据不一样,如果当天没有,则都为0,如上表中的al99.00
      

  5.   

    dh9450(荡尽尘埃) 谢谢你,你的意思对可如果al99.00品味的没有,则在表二中没有记录
      

  6.   

    ...没有也要有记录吗 
    最后再放条SQL加进去好了
    你应该有个品位表的吧
      

  7.   

    对的,应该有个品味表,下面是语句,其实表二是不需要的,SQL语句可以直接查询出来你要的结果Connected to Oracle8i Enterprise Edition Release 8.1.6.0.0 
    Connected as scott
    SQL> 
    SQL> create table tab1
      2   (槽号 char(3),
      3    品味 char(10),
      4    日期 char(10)
      5    );Table createdSQL> insert into tab1
      2  select '101','al99.90', '2005-1-30' from dual;1 row insertedSQL> insert into tab1
      2  select '102','al99.90', '2005-1-30' from dual;1 row insertedSQL> commit;Commit completeSQL> create table pingwei
      2  (品味 char(10));Table createdSQL> insert into pingwei values ('al99.90');1 row insertedSQL> insert into pingwei values ('al99.85');1 row insertedSQL> insert into pingwei values ('al99.70A');1 row insertedSQL> insert into pingwei values ('AL99.70');1 row insertedSQL> insert into pingwei values ('AL99.60');1 row insertedSQL> insert into pingwei values ('AL99.50');1 row insertedSQL> insert into pingwei values ('AL99.00');1 row insertedSQL> commit;Commit completeSQL> select b.品味,nvl(a.车间1,0),nvl(车间2,0),日期 from(
      2  select 品味,sum(车间1)车间1,sum(车间2)车间2,日期 from
      3  (select 品味,decode(floor(to_number(substr(槽号,1,1))/3),0,1,0) 车间1,
      4  decode(floor(to_number(substr(槽号,1,1))/3),1,1,0) 车间2,
      5  日期 from tab1) a
      6  group by 品味,日期) a,pingwei b
      7  where a.品味(+)=b.品味;品味       NVL(A.车间1,0) NVL(车间2,0) 日期
    ---------- -------------- ------------ ----------
    AL99.00                 0            0 
    AL99.50                 0            0 
    AL99.60                 0            0 
    AL99.70                 0            0 
    al99.70A                0            0 
    al99.85                 0            0 
    al99.90                 2            0 2005-1-307 rows selectedSQL> drop table tab1;Table droppedSQL> drop table pingwei;Table droppedSQL>
      

  8.   

    其中这部分就是你要的语句,
    当然可以前面加个create 表2 as 来生成你要的表,
    或者加truncate 表2;和insert into 表2来完成
    select b.品味,nvl(a.车间1,0),nvl(车间2,0),日期 from(
    select 品味,sum(车间1)车间1,sum(车间2)车间2,日期 from
    (select 品味,decode(floor(to_number(substr(槽号,1,1))/3),0,1,0) 车间1,
    decode(floor(to_number(substr(槽号,1,1))/3),1,1,0) 车间2,
    日期 from tab1) a
    group by 品味,日期) a,pingwei b
    where a.品味(+)=b.品味;