表结构: 查询结果:名字 人数 名字 人数
--------------- -----------
海淀 3 市区 6
崇文 1 海淀 3
西城 2 崇文 1
顺义 3 西城 2
平谷 3
房山 3
大兴 3
昌平 3
怀柔 3
密云 3要求: 查询出市区的总人数(海淀,崇文,西城的综合) *查询出的结果在上面
请高手帮着解决下
--------------- -----------
海淀 3 市区 6
崇文 1 海淀 3
西城 2 崇文 1
顺义 3 西城 2
平谷 3
房山 3
大兴 3
昌平 3
怀柔 3
密云 3要求: 查询出市区的总人数(海淀,崇文,西城的综合) *查询出的结果在上面
请高手帮着解决下
union all
select 名字,人数 from tb where 名字 in('海淀','崇文','西城')
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-09-29 20:05:39
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([名字] varchar(4),[人数] int)
insert [tb]
select '海淀',3 union all
select '崇文',1 union all
select '西城',2 union all
select '顺义',3 union all
select '平谷',3 union all
select '房山',3 union all
select '大兴',3 union all
select '昌平',3 union all
select '怀柔',3 union all
select '密云',3
--------------开始查询--------------------------
select 名字='市区',人数=sum(人数) from tb where 名字 in('海淀','崇文','西城')
union all
select 名字,人数 from tb where 名字 in('海淀','崇文','西城')
----------------结果----------------------------
/* 名字 人数
---- -----------
市区 6
海淀 3
崇文 1
西城 2(4 行受影响)
*/
select sum(人数) 人数
from (select top 3 * from tb) t
/*
人数
-----------
6
*/
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-09-29 20:05:39
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([名字] varchar(4),[人数] int)
insert [tb]
select '海淀',3 union all
select '崇文',1 union all
select '西城',2 union all
select '顺义',3 union all
select '平谷',3 union all
select '房山',3 union all
select '大兴',3 union all
select '昌平',3 union all
select '怀柔',3 union all
select '密云',3
--------------开始查询--------------------------
select id=identity (int),* into #t from tb
select 名字='市区',人数=sum(人数) from #t where id between 1 and 3
union all
select 名字,人数 from #t where id between 1 and 3
----------------结果----------------------------
/* 名字 人数
---- -----------
市区 6
海淀 3
崇文 1
西城 2(4 行受影响)
*/
drop table tb
go
create table tb(名字 nvarchar(20),人数 int)
goinsert into tb
select '海淀',3 union all
select '崇文',1 union all
select '西城',2 union all
select '顺义',3 union all
select '平谷',3 union all
select '房山',3 union all
select '大兴',3 union all
select '昌平',3 union all
select '怀柔',3 union all
select '密云',3
Select (case when grouping(名字)=0 then 名字 else '城市' end) 名字 ,Sum(人数) 人数
from tb
where 名字 in ('海淀','崇文','西城')
group by 名字 with rollup/*
名字 人数
-------------------- -----------
崇文 1
海淀 3
西城 2
城市 6
*/
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-09-29 20:05:39
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([名字] varchar(4),[人数] int)
insert [tb]
select '海淀',3 union all
select '崇文',1 union all
select '西城',2 union all
select '顺义',3 union all
select '平谷',3 union all
select '房山',3 union all
select '大兴',3 union all
select '昌平',3 union all
select '怀柔',3 union all
select '密云',3
--------------开始查询--------------------------
select 名字,人数 from tb where 名字 in('海淀','崇文','西城')
union all
select 名字='市区',人数=sum(人数) from tb where 名字 in('海淀','崇文','西城')
----------------结果----------------------------
/* 名字 人数
---- -----------
海淀 3
崇文 1
西城 2
市区 6(4 行受影响)
*/