日期:2014-05-17 浏览次数:20444 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-12-24 16:48:06
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go
create table [test]([ID] int,[name] varchar(4),[need] int,[had] int)
insert [test]
select 1,'刘一',1000,900 union all
select 2,'陈二',2000,1680 union all
select 3,'张三',900,1440 union all
select 4,'李四',1800,2500 union all
select 5,'王五',1550,1350 union all
select 6,'赵六',1240,1000 union all
select 7,'孙七',1530,1530 union all
select 8,'周八',900,950 union all
select 9,'吴九',3570,3320 union all
select 10,'郑十',1730,2000
--------------开始查询--------------------------
select name,数量 from (select ID, name,