我现在有个基础型号表为A表, 我又创建了个B表为型号组表,B表的数据来源A表, 现在我创建了2个型号组为“型号A”,“型号B”,
A表  
prac1 型号     flag  
W-1   AAA      3
W-2   BBB      3
W-3   CCC      3
W-4   DDD      3
T-5   EEE      4
T-6   FFF      4
T-7   GGG      4
C1    QC       5
C2    EV       5
C3    QX       5
C4    BBE      5
B表
组名    型号   属性
型号A   W-1    门套
型号A   W-2    门套
型号A   W-3    门套
型号B   EEE    木门
型号B   FFF    木门
型号C   QC     装饰材料
型号C   EV     装饰材料
型号C   QX     装饰材料
在具体匹配型号的时候是排除选种的型号。也就是说根据排除B表数据into到C表, 才能使用  , 
B表属性如果为“门套”那么A表就取 SELECT distinct prac1 型号 FROM base_data where flag=3 and prac1<>''
B表属性如果为“木门”那么A表就取 SELECT distinct bname 型号 FROM base_data where flag=4 and bname<>''
B表属性如果为“装饰材料”那么A表就取 select  distinct bname 型号 from base_data  where flag=5  and  (prac1 like '%线%' or prac1 like '%板%' or prac1 like '%条%') and bname<>''
最后结果为
C表
组名    型号   属性
型号A   W-4    门套
型号B   GGG    木门
型号C   BBE    装饰材料帮我分析下写出来。

解决方案 »

  1.   


    select a.prac1,a.型号,a.flag,b.属性,b.组名
    into #tb
    from a left join b on a.型号 = b.型号update t
    set t.属性 = (select max(属性) from #tb where flag = t.flag),
    t.组名 = (select max(组名) from #tb where flag = t.flag)
    from #tb tinsert into c
    select 组名,型号,属性
    from #tb e
    where not exists (select 1 from b where 组名 = e.组名 and 型号 = e.型号)drop table #tb
      

  2.   

    insert into c表
    select a.组名,a.型号,b.属性
    from B表 a,(select distinct 组名,属性 from B表) b
    where a.组名=b.组名
     and not exists(select 1 from A表 where prac1=a.型号)
      

  3.   

    可否给点创建表的sql?应该是对的吧
      

  4.   

    楼主你本身给的数据有问题:
    A表   
    prac1 型号 flag   
    W-1 AAA 3
    W-2 BBB 3
    W-3 CCC 3
    W-4 DDD 3
    T-5 EEE 4
    T-6 FFF 4
    T-7 GGG 4
    C1 QC 5
    C2 EV 5
    C3 QX 5
    C4 BBE 5
    B表
    组名 型号 属性
    型号A W-1 门套
    型号A W-2 门套
    型号A W-3 门套
    型号B EEE 木门
    型号B FFF 木门
    型号C QC 装饰材料
    型号C EV 装饰材料
    型号C QX 装饰材料看看你B表型号对应的A表,这样能出来什么!
      

  5.   

    prac1 like '%线%' or prac1 like '%板%' or prac1 like '%条%'
    这些是从哪里来的哦?
      

  6.   


    create table a   
    (prac1 varchar(10),型号 varchar(10),flag int)
    insert into a
    select 'W-1','AAA',3 union all
    select 'W-2','BBB',3 union all
    select 'W-3','CCC',3 union all
    select 'W-4','DDD',3 union all
    select 'T-5','EEE',4 union all
    select 'T-6','FFF',4 union all
    select 'T-7','GGG',4 union all
    select 'C1','QC',5 union all
    select 'C2','EV',5 union all
    select 'C3','QX',5 union all
    select 'C4','BBE',5
    go
    create table b
    (组名 varchar(10),型号 varchar(10),属性 varchar(10))
    insert into b
    select '型号A','W-1','门套' union all
    select '型号A','W-2','门套' union all
    select '型号A','W-3','门套' union all
    select '型号B','EEE','木门' union all
    select '型号B','FFF','木门' union all
    select '型号C','QC','装饰材料' union all
    select '型号C','EV','装饰材料' union all
    select '型号C','QX','装饰材料'
    goselect a.prac1,a.型号,a.flag,b.属性,b.组名
        into #tb
    from a left join b on b.型号 = (case when a.flag = 3 then a.prac1 else a.型号 end)update t
    set t.属性 = (select max(属性) from #tb where flag = t.flag),
        t.组名 = (select max(组名) from #tb where flag = t.flag)
    from #tb tselect 组名,型号,属性
    into c
    from #tb e
    where not exists (select 1 from b where 组名 = e.组名 
    and 型号 = (case when e.flag = 3 then e.prac1 else e.型号 end))select *
    from cdrop table a,b,c,#tb
    /***********组名         型号         属性
    ---------- ---------- ----------
    型号A        DDD        门套
    型号B        GGG        木门
    型号C        BBE        装饰材料(3 行受影响)
      

  7.   

    declare @t1 table(prac1 varchar(10), 型号 varchar(10), flag int)
    declare @t2 table(组名 varchar(10), 型号 varchar(10), 属性 varchar(10))insert into @t1(prac1, 型号, flag)
    select 'W-1','AAA',3 union all
    select 'W-2','BBB',3 union all
    select 'W-3','CCC',3 union all
    select 'W-4','DDD',3 union all
    select 'T-5','EEE',4 union all
    select 'T-6','FFF',4 union all
    select 'T-7','GGG',4 union all
    select 'C1','QC',5 union all
    select 'C2','EV',5 union all
    select 'C3','QX',5 union all
    select 'C4','BBE',5insert into @t2(组名, 型号, 属性)
    select '型号A','W-1','门套' union all
    select '型号A','W-2','门套' union all
    select '型号A','W-3','门套' union all
    select '型号B','EEE','木门' union all
    select '型号B','FFF','木门' union all
    select '型号C','QC','装饰材料' union all
    select '型号C','EV','装饰材料' union all
    select '型号C','QX','装饰材料'select '型号' + case when left(prac1, 1) = 'W' then 'A'
     when left(prac1, 1) = 'T' then 'B'
     when left(prac1, 1) = 'C' then 'C' end 组名,
    case when left(prac1, 1) = 'W' then a.prac1 else a.型号 end 型号,
    case when left(prac1, 1) = 'W' then '门套'
     when left(prac1, 1) = 'T' then '木门'
     when left(prac1, 1) = 'C' then '装饰材料' end 属性
    from @T1 a left join @T2 b on case when left(prac1, 1) = 'W' then a.prac1 else a.型号 end = b.型号
    where b.型号 is null
      

  8.   

    哎, 我觉得设计这个SQL的也很闹心, 我是后来这个公司。 他们表都见好了, 我就只能用了。 添加数据还来个非。 取所有不在组里的型号, not in就行, 但是基础表里。 乱78遭数据一带堆,
      

  9.   

    中午搞定了。  用的while 写了个循环,  比指针舒服多了。 呵呵,谢谢大家啊