一个纵表转横表的问题,请指教!

Jamesczh 2005-10-28 09:55:10
一个纵表转横表的问题:

现有表baseinfo结构如下:

区域 企业注册资金(万) 数量(家)
浦东新区 200 100
卢湾区 1000 6
浦东新区 800 50
黄浦区 400 60
浦东新区 1600 20
卢湾区 450 30
奉贤区 1800 2
黄浦区 600 10
卢湾区 1900 3

现在要统计的结果如下:

浦东小企 浦东大企 浦东合计 浦西小企业 浦西大企 浦西合计 上海小企 上海大企 上海合计
150 20 170 100 11 111 250 31 281

说明:
A、 浦东浦西的划分:区域等于浦东新区的表示浦东,区域不等于浦东新区的表示浦西。
B、 小企和大企的划分:企业注册资金大于等于1000万的为大企业,小于1000万的为小企业。
谢谢先!!
...全文
108 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2005-10-28
  • 打赏
  • 举报
回复
--生成测试数据
create table baseinfo(区域 varchar(100),[企业注册资金(万)] int,[数量(家)] int)
insert into baseinfo select '浦东新区',200 ,100
insert into baseinfo select '卢湾区 ',1000,6
insert into baseinfo select '浦东新区',800 ,50
insert into baseinfo select '黄浦区 ',400 ,60
insert into baseinfo select '浦东新区',1600,20
insert into baseinfo select '卢湾区 ',450 ,30
insert into baseinfo select '奉贤区 ',1800,2
insert into baseinfo select '黄浦区 ',600 ,10
insert into baseinfo select '卢湾区 ',1900,3

--执行交叉表查询
select
浦东小企 = sum(case when 区域 ='浦东新区' and [企业注册资金(万)]< 1000 then [数量(家)] else 0 end),
浦东大企 = sum(case when 区域 ='浦东新区' and [企业注册资金(万)]>=1000 then [数量(家)] else 0 end),
浦东合计 = sum(case when 区域 ='浦东新区' then [数量(家)] else 0 end),
浦西小企 = sum(case when 区域!='浦东新区' and [企业注册资金(万)]<1000 then [数量(家)] else 0 end),
浦西大企 = sum(case when 区域!='浦东新区' and [企业注册资金(万)]>1000 then [数量(家)] else 0 end),
浦西合计 = sum(case when 区域!='浦东新区' then [数量(家)] else 0 end),
上海小企 = sum(case when [企业注册资金(万)]< 1000 then [数量(家)] else 0 end),
上海大企 = sum(case when [企业注册资金(万)]>=1000 then [数量(家)] else 0 end),
上海合计 = sum([数量(家)])
from
baseinfo


--删除测试数据
drop table baseinfo


--输出结果
/*
浦东小企 浦东大企 浦东合计 浦西小企 浦西大企 浦西合计 上海小企 上海大企 上海合计
150 20 170 100 11 111 250 31 281
*/
vivianfdlpw 2005-10-28
  • 打赏
  • 举报
回复
declare @tb table
(
区域 varchar(20),
企业注册资金 int,
数量 int
)
insert @tb
select '浦东新区', 200, 100 union
select '卢湾区', 1000, 6 union
select '浦东新区', 800, 50 union
select '黄浦区', 400, 60 union
select '浦东新区', 1600, 20 union
select '卢湾区', 450, 30 union
select '奉贤区', 1800, 2 union
select '黄浦区', 600, 10 union
select '卢湾区', 1900, 3

--查询
select sum(case when 区域='浦东新区' and 企业注册资金<1000
then 数量 else 0 end) as '浦东小企'
,sum(case when 区域='浦东新区' and 企业注册资金>=1000
then 数量 else 0 end) as '浦东大企'
,sum(case when 区域='浦东新区'
then 数量 else 0 end) as '浦东合计'
,sum(case when 区域<>'浦东新区' and 企业注册资金<1000
then 数量 else 0 end) as '浦西小企'
,sum(case when 区域<>'浦东新区' and 企业注册资金>=1000
then 数量 else 0 end) as '浦西大企'
,sum(case when 区域<>'浦东新区' then 数量 else 0 end) as '浦西合计'
,sum(case when 企业注册资金<1000 then 数量 else 0 end) as '上海小企'
,sum(case when 企业注册资金>=1000 then 数量 else 0 end) as '上海大企'
,sum(数量) as '上海合计'
from @tb

--结果
/*
浦东小企 浦东大企 浦东合计 浦西小企 浦西大企 浦西合计 上海小企 上海大企 上海合计
----------- ----------- ----------- ----------- ----------- ----------- ----------- -----
150 20 170 100 11 111 250 31 281

(1 row(s) affected)
*/
子陌红尘 2005-10-28
  • 打赏
  • 举报
回复
--生成测试数据
create table baseinfo(区域 varchar(100),[企业注册资金(万)] int,[数量(家)] int)
insert into baseinfo select '浦东新区',200 ,100
insert into baseinfo select '卢湾区 ',1000,6
insert into baseinfo select '浦东新区',800 ,50
insert into baseinfo select '黄浦区 ',400 ,60
insert into baseinfo select '浦东新区',1600,20
insert into baseinfo select '卢湾区 ',450 ,30
insert into baseinfo select '奉贤区 ',1800,2
insert into baseinfo select '黄浦区 ',600 ,10
insert into baseinfo select '卢湾区 ',1900,3

--执行交叉表查询
select
浦东小企 = sum(case when 区域 ='浦东新区' and [企业注册资金(万)]< 1000 then [数量(家)] else 0 end),
浦东大企 = sum(case when 区域 ='浦东新区' and [企业注册资金(万)]>=1000 then [数量(家)] else 0 end),
浦东合计 = sum(case when 区域 ='浦东新区' then [数量(家)] else 0 end),
浦西小企 = sum(case when 区域!='浦东新区' and [企业注册资金(万)]<1000 then [数量(家)] else 0 end),
浦西大企 = sum(case when 区域!='浦东新区' and [企业注册资金(万)]>1000 then [数量(家)] else 0 end),
浦西合计 = sum(case when 区域!='浦东新区' then [数量(家)] else 0 end),
上海小企 = sum(case when [企业注册资金(万)]< 1000 then [数量(家)] else 0 end),
上海大企 = sum(case when [企业注册资金(万)]>=1000 then [数量(家)] else 0 end),
上海合计 = sum([数量(家)])
from
baseinfo


--删除测试数据
drop table baseinfo


--输出结果
/*
浦东小企 浦东大企 浦东合计 浦西小企业 浦西大企 浦西合计 上海小企 上海大企 上海合计
150 20 170 100 11 111 250 31 281
*/
子陌红尘 2005-10-28
  • 打赏
  • 举报
回复
select
浦东小企 = sum(case when 区域 ='浦东新区' and [企业注册资金(万)]< 1000 then [数量(家)] else 0 end),
浦东大企 = sum(case when 区域 ='浦东新区' and [企业注册资金(万)]>=1000 then [数量(家)] else 0 end),
浦东合计 = sum(case when 区域 ='浦东新区' and then [数量(家)] else 0 end),
浦西小企 = sum(case when 区域!='浦东新区' and [企业注册资金(万)]<1000 then [数量(家)] else 0 end),
浦西大企 = sum(case when 区域!='浦东新区' and [企业注册资金(万)]>1000 then [数量(家)] else 0 end),
浦西合计 = sum(case when 区域!='浦东新区' and then [数量(家)] else 0 end),
上海小企 = sum(case when [企业注册资金(万)]< 1000 then [数量(家)] else 0 end),
上海大企 = sum(case when [企业注册资金(万)]>=1000 then [数量(家)] else 0 end),
上海合计 = sum([数量(家)])
from
baseinfo

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧