日期:2014-05-18  浏览次数:20398 次

一個很有難度的sql
select     pnl,date_piece,model_no,factsal  
    from     wg_yyjz  
  where     date_piece   like   '200703% ';
----------------------------------------------
pnl           date_piece         model_no     factsal

05431 20070301 PM-192 76.69
05434 20070312 BM-78 71.53
05461 20070322 130 55.16
05461 20070322 -002 55.16
05461 20070322 192 55.16
05479 20070324 -192-1 56.38
05485 20070325 78 66.59
05488 20070328 78 57.08
05488 20070328 78.1 57.08
-------------------------------------------
我想得到的結果是:
(相同工號同一天不同model_no工人(pnl)的factsal有重復(因數據量大,可能有多個重復factsal)
  的就賦值為0,只保留一個factsal(為一天的實際工資值))
-------------------------------------------
pnl           date_piece         model_no     factsal

05431 20070301 PM-192 76.69
05434 20070312 BM-78 71.53
05461 20070322 130 55.16
05461 20070322 -002 0
05461 20070322 192 0
05479 20070324 -192-1 56.38
05485 20070325 78 66.59
05488 20070328 78 57.08
05488 20070328 78.1 0
-------------------------------------------
怎麼實現?help!

------解决方案--------------------
--保留model_no相對最小的一條:
update wg_yyjz
set factsal=0
where date_piece like '200703% '
and exists(select 1 from wg_yyjz a
where a.pnl=wg_yyjz.pnl
and a.date_piece=wg_yyjz.dete_piece
and a.factsal=wg_yyjz.factsal
and a.model_no <wg_yyjz.model_no)