商品編碼 數量 單價 供應商
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
)
本人認爲方法有更多種,這也算是小技巧,記下來,說不定以後就能用到