求高效加工最近日期,再最大单价视图           
环境:SQL2000
要求:
1、首先计算出该加工单位、该加工产品、该产品工序的最大日期的单价
2、如果同一天加工单位、产品、工序、单价 均相同时取最大单价
3、加工单位、产品、工序、单价 不能有重复行来源:  视图view_wwjgjgdj
加工单位, 日期, 产品, 加工工序, 加工单价
traderid,  billdate,  materialid,  vipbmclid,  price
10001   ,2013-03-01 ,   3001    ,    1      ,  2.3  ----前面4个字段重复
10001   ,2013-03-01 ,   3001    ,    1      ,  2.7  ----前面4个字段重复
10001   ,2013-03-08 ,   3001    ,    2      ,   3
10001   ,2013-06-05 ,   3001    ,    1      ,   4   
10001   ,2013-06-07 ,   3001    ,    1      ,   3.3   ---完全重复
10001   ,2013-06-07 ,   3001    ,    1      ,   3.3   ---完全重复  
2008    ,2013-06-01 ,   7008    ,    1      ,   4.1   ----不同加工单位一、求视图view  vip_wwjgdjbbmx    select  * from   vip_wwjgdjbbmx 
traderid,  billdate,  materialid,  vipbmclid,  price
10001   ,2013-06-07 ,   3001    ,    1      ,   3.3   
2008    ,2013-06-01 ,   7008    ,    1      ,   4.1   

解决方案 »

  1.   


    select distinct * from tb a 
    where price=(select max(price) from tb b where traderid=a.traderid and 
    billdate=(select max(billdate) from tb where b.traderid=traderid))不知道2000能不能用
      

  2.   


    好像没有考虑  vipbmclid 加工工序
      

  3.   


    select 10001 traderid,'2013-03-01' billdate,3001 materialid,1 vipbmclid,2.3 price ----前面4个字段重复
    into #t
    union all select 10001,'2013-03-01',3001,1,2.7----前面4个字段重复
    union all select 10001,'2013-03-08',3001,2, 3
    union all select 10001,'2013-06-05',3001,1, 4 
    union all select 10001,'2013-06-07',3001,1, 3.3 ---完全重复
    union all select 10001,'2013-06-07',3001,1, 3.3 ---完全重复
    union all select 2008,'2013-06-01',7008,1, 4.1 ----不同加工单位select distinct t1.*
    from 
    (
    select traderid,materialid,vipbmclid,max(billdate) billdate
    from #t
    group by traderid,materialid,vipbmclid
    ) t
    join #t t1
    on t.traderid=t1.traderid and t.materialid=t1.materialid
    and t.vipbmclid=t1.vipbmclid and t.billdate=t1.billdate这样?这样符合你的文字要求,但是与你列出的结果不符合。
      

  4.   


    -- 修改了一下测试数据以求效果更明显select 10001 traderid,'2013-03-01' billdate,3002 materialid,1 vipbmclid,2.3 price ----前面4个字段重复
    into #t
    union all select 10001,'2013-03-01',3002,1,2.7----前面4个字段重复
    union all select 10001,'2013-03-08',3001,2, 3
    union all select 10001,'2013-06-05',3001,1, 4 
    union all select 10001,'2013-06-07',3001,1, 3.3 ---完全重复
    union all select 10001,'2013-06-07',3001,1, 3.3 ---完全重复
    union all select 2008,'2013-06-01',7008,1, 4.1 ----不同加工单位select t1.traderid, t1.materialid,t1.vipbmclid,t1.billdate, MAX(t1.price) AS Price
    from 
    (
    select traderid,materialid,vipbmclid,max(billdate) billdate
    from #t
    group by traderid,materialid,vipbmclid
    ) t
    join #t t1
    on t.traderid=t1.traderid and t.materialid=t1.materialid
    and t.vipbmclid=t1.vipbmclid and t.billdate=t1.billdate 
    GROUP BY t1.traderid, t1.materialid,t1.vipbmclid,t1.billdate  drop table #t