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

这样的查询如何写??
这样的SQL如何得到?

标项                                 货品明细码               供应商ID                 报价价格           报价时间
bp_id                             bp_pid               bp_salerid           bp_price       bp_bjdate
20070202131249_016   200702017_008     20070123133123     100       2007-2-6   10:21:15
20070202131249_016   200702017_011     20070123133123     50       2007-2-6   10:21:15
20070202131249_016   200702017_008     20070123133103     200       2007-2-6   10:19:15
20070202131249_016   200702017_011     20070123133103     150       2007-2-6   10:19:15  

我想得到供应商的ID
标项20070202131249_016   所有物品报价总和最近的供应商ID   如果两个供应商报价相同取
报价时间早的一个
SQL如何写??

------解决方案--------------------
select bp_id,bp_salerid,sum(bp_price) as bp_price,min(bp_bjdate) as bp_bjdate into # from tablename group by bp_id,bp_salerid
select * from # t where not exists(select 1 from # where bp_price> t.bp_price and bp_bjdate <t.bp_bjdate and bp_id=t.bp_id)
drop table #