select max(t.cinvcode),t.钢卷号,max(t.仓库货位) as '仓库货位',max(t.片宽) as '片宽',max(t.片长) as '片长',max(t.材质说明) as '材质说明',max(t.重量) as '重量',max(t.片厚) as '片厚',max(t.材质) as '材质',max(t.问题描述) as '问题描述', max(t.锁库人) as '锁库人',max(t.配料单号) as '配料单号',max(t.技术信息) as '技术信息',max(t.配料日期) as '配料日期' 
  from 
(select t.cinvcode,t.钢卷号,t.仓库货位,t.片宽,t.片长,t.材质说明,t.重量,t.片厚,t.材质,t.问题描述, '' as '锁库人','' as '配料单号',t.技术信息,'' as '配料日期' 
from 
(select b.cinvcode as 'cinvcode',d.cbatch as '钢卷号',e.cwhname as '仓库货位',d.cbatchproperty2 as '片宽','' as '片长',max(d.cbatchproperty7) as '材质说明',d.iQuantity as '重量',d.cbatchproperty1 as '片厚',d.cbatchproperty6 as '材质',max(d.cbatchproperty8) as '问题描述',max(d.cbatchproperty9) as '技术信息',b.cinvname,b.cinvstd,d.cbatchproperty3 
  from  inventory b ,rdrecords d,Warehouse e 
  where  b.cinvdefine3 = 1  and  isnull(d.cbatch,'') <> '' and d.iQuantity>0
  group by b.cinvcode,d.cbatch,b.cinvname,b.cinvstd,d.iQuantity,d.cbatchproperty1,d.cbatchproperty2,d.cbatchproperty3,e.cwhname,d.cbatchproperty6) t 
   ) t,UFPTTX..LockStock
  where 1=1  and t.钢卷号 not in(UFPTTX..LockStock.cbatch)
  group by t.钢卷号加了not in之后很慢,求高手优化

解决方案 »

  1.   

    ,子查询放到with里面。建议楼主多用表连接,2个子查询嵌套,Oh my God
      

  2.   

    select t.钢卷号,
    max(t.cinvcode),
    max(t.仓库货位) as '仓库货位',
    max(t.片宽) as '片宽',
    max(t.片长) as '片长',
    max(t.材质说明) as '材质说明',
    max(t.重量) as '重量',
    max(t.片厚) as '片厚',
    max(t.材质) as '材质',
    max(t.问题描述) as '问题描述', 
    max(t.锁库人) as '锁库人',
    max(t.配料单号) as '配料单号',
    max(t.技术信息) as '技术信息',
    max(t.配料日期) as '配料日期'  
    from(
    select b.cinvcode as 'cinvcode',
    d.cbatch as '钢卷号',
    e.cwhname as '仓库货位',
    d.cbatchproperty2 as '片宽',
    '' as '片长',  --个人觉得把这四个"列"放进去 求 max 真有点匪夷所思,不是空么,求了 max 不还是空么?!
    '' as '锁库人',
    '' as '配料单号',
    '' as '配料日期',
    max(d.cbatchproperty7) as '材质说明',
    d.iQuantity as '重量',
    d.cbatchproperty1 as '片厚',
    d.cbatchproperty6 as '材质',
    max(d.cbatchproperty8) as '问题描述',
    max(d.cbatchproperty9) as '技术信息',
    b.cinvname,
    b.cinvstd,
    d.cbatchproperty3  
        from inventory b ,rdrecords d,Warehouse e  
        where b.cinvdefine3 = 1 and isnull(d.cbatch,'') <> '' and d.iQuantity>0
    and not exists(select 1 from UFPTTX..LockStock where cbatch=d.cbatck)
        group by b.cinvcode,d.cbatch,b.cinvname,b.cinvstd,d.iQuantity,d.cbatchproperty1,d.cbatchproperty2,d.cbatchproperty3,e.cwhname,d.cbatchproperty6
    )t
      

  3.   

    就这样,看上去还是怪异.
    1.可以进一步缩成一个语句,不用子查询,对列进行了 max 后再max真的有点...
    2.还有,字符串型的列也求 max ? 能说说 '此型号已停产' 和 '该型号已停产' 的差别吗?