日期:2014-05-17 浏览次数:20669 次
----------------------------
-- Author :TravyLee(物是人非事事休,欲语泪先流!)
-- Date :2012-09-12 14:33:38
-- Version:
-- Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
-- Oct 14 2005 00:33:37
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 6.1 (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go
create table [test]([单位] varchar(6),[发货单号] int,[销售数量] numeric(14,10),[已出库数量] numeric(14,10))
insert [test]
select '箱(12)',120800815,3000.0000000000,3000.0000000000 union all
select '箱(12)',120800672,2500.0000000000,596.0000000000 union all
select '箱(12)',120800672,2500.0000000000,1800.0000000000 union all
select '箱(12)',120800672,2500.0000000000,104.0000000000 union all
select '箱(24)',120800870,400.0000000000,400.0000000000
go
select
distinct [发货单号],
sum([销售数量])over(partition by getdate()) as [销售数量],
sum([已出库数量])over(partition by [发货单号]) as [已出库数量]
from
test
/*
[发货单号] [销售数量] [已出库数量]
--------------------------
120800672 10900.0000000000 2500.0000000000
120800815 10900.0000000000 3000.0000000000
120800870 10900.0000000000 400.0000000000
*/
不晓得你具体要什么结果