sql小結.11.24.2006

商品編碼     數量    單價       供應商
001         20       0.3       工商企業
001         50       0.1       AB企業
002         100      1.2       OK企業
003         200      2.4       AB企業
003         500      1.2       SQ企業

寫查詢語句,得到每種商品單價爲最低一條記錄,結果如下:

商品編碼     數量    單價       供應商
001         50       0.1       AB企業
002         100      1.2       OK企業
003         500      1.2       SQ企業

方案:

 select *
from tablesql a,
(select [商品編碼],min([單價])as [單價] from tablesql group by [商品編碼]) b
where a.[商品編碼] = b.[商品編碼] and a.[單價]= b.[單價]

select *
from product a
where not exists(
 select 1
 from product
 where id=a.id and price<a.price
)

本人認爲方法有更多種,這也算是小技巧,記下來,說不定以後就能用到

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章