首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一個很有難度的sql解决方法

2012-03-21 
一個很有難度的sqlselectpnl,date_piece,model_no,factsalfromwg_yyjzwheredate_piecelike200703% ------

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

0543120070301PM-19276.69
0543420070312BM-7871.53
054612007032213055.16
0546120070322-00255.16
054612007032219255.16
0547920070324-192-156.38
05485200703257866.59
05488200703287857.08
054882007032878.157.08
-------------------------------------------
我想得到的結果是:
(相同工號同一天不同model_no工人(pnl)的factsal有重復(因數據量大,可能有多個重復factsal)
  的就賦值為0,只保留一個factsal(為一天的實際工資值))
-------------------------------------------
pnl           date_piece         model_no     factsal

0543120070301PM-19276.69
0543420070312BM-7871.53
054612007032213055.16
0546120070322-0020
05461200703221920
0547920070324-192-156.38
05485200703257866.59
05488200703287857.08
054882007032878.10
-------------------------------------------
怎麼實現?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)

热点排行