急~~求一个SQL写法!
查询一个订单表:同一个订单编号时:配送费用只计费一次
表中的数据如:
Create Table dbo.shop_ordertb (
czid int IDENTITY (1, 1) PRIMARY KEY,-- 自动编号
orderid varchar(20) NULL , -- 订单号
cartID int default 0, -- 购物车ID
deliveryprice int default 0 -- 配送费用
)
表中的数据如:
czid orderid cartID deliveryprice
1009 Q07419-02605 2838 80
1010 Q07419-02605 2839 80
1011 Q07419-02605 2840 80
要得到查询的结果是:
表中的数据如:
czid orderid cartID deliveryprice
1009 Q07419-02605 2838 80
1010 Q07419-02605 2839 0
1011 Q07419-02605 2840 0
应该如何写SQL语句,orderid是动态的?
------解决方案--------------------create table #T(czid int, orderid varchar(100), cartID int,deliveryprice int)
insert into #t select 1009, 'Q07419-02605 ', 2838,80
insert into #t select 1010, 'Q07419-02605 ', 2839,80
insert into #t select 1011, 'Q07419-02605 ', 2840,80
select czid,orderid,cartID,(case when cartID=(select min(cartID) from #t where orderid=a.orderid) then deliveryprice else 0 end) as deliveryprice
from #t as A
drop table #t