日期:2014-05-17 浏览次数:20634 次
-- data
if object_id('tempdb.dbo.#') is not null drop table #
create table #(idx int, fname varchar(8), fmodel float, fqty float, p varchar(8), sumfqty float)
insert into #
select 1, 'A1', 0.20000, 89.00000, 'K', 150.00000 union all
select 2, 'A1', 0.20000, 89.00000, 'K', 150.00000 union all
select 3, 'A2', 0.50000, 20.00000, 'K1', 10.00000 union all
select 4, 'A2', 0.50000, 20.00000, 'K1', 10.00000
-- query
;with tmp as
(
select *, v=sumfqty-(select isnull(sum(fqty),0) from # where&nbs