SQL語句集合

football2006發表於2008-07-10

一、
表table中的資料如下:
有3個欄位:
欄位a 欄位b 欄位c
1 11 4
1 12 5
1 13 6

2 21 7
2 22 8
2 23 9

3 31 10
...

問題:
如何取得:欄位a對應的最大欄位b的記錄,具體如下:
1 13 6
2 23 9

SQL:
select a,b,c from table where b in (select max(b) from table group by a);
或:
select u.a, u.b, u.c
from (select rowber_number() over(partition by a order by b desc) rn,
a.a,
a.b.a.c table a) u
where u.rn = 1

二、
select fid from t_st_weighnoticebillentryqi where fnumber in
(
select * from
(select fnumber from t_st_weighnoticebillentryqi t where to_char(t.fbizdate,'yyyy/mm/dd hh24:mi:ss') > '2008/06/15 07.13.52' and
to_char(t.fbizdate,'yyyy/mm/dd hh24:mi:ss') < '2008/07/01 07.13.52'
and t.fmaterialid = 'MPwdnwEQEADgB2cNwKgyAkQJ5/A='
minus
select substr(fsoucebillnumber,1,15) from t_st_qireportentry)
)

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8119050/viewspace-1007092/,如需轉載,請註明出處,否則將追究法律責任。