求SQL语句,50分在线等

temolo 2006-08-30 02:36:16
有表如下
Table1
地区 人口总数
武汉 100
上海 200

Table2
地区 病因 病人数
武汉 感冒 50
武汉 咳嗽 20
武汉 头疼 30
上海 感冒 80
上海 咳嗽 120

问题:由上述两个表通过SQL语句返回如下结果集,最好不使用中间表
Table3
地区 感冒 咳嗽 头疼 人口总数
武汉 50 20 30 100
上海 80 120 0 200
...全文
218 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mabuchi 2006-09-02
  • 打赏
  • 举报
回复
shou
九斤半 2006-08-30
  • 打赏
  • 举报
回复
create table Table1
(
地区 varchar(10),
人口总数 int
)
insert table1
select '武汉',100 union all
select '上海',200
--select * from table1

create table Table2
(
地区 varchar(10),
病因 varchar(10),
病人数 int
)
insert table2
select '武汉','感冒',50 union all
select '武汉','咳嗽',20 union all
select '武汉','头疼',30 union all
select '上海','感冒',80 union all
select '上海','咳嗽',120
--select * from table2

--用于:交叉表的列数是不确定的
declare @sql varchar(8000)

SELECT @sql = 'select B.*,A.人口总数 from table1 A inner join (select 地区,'
select @sql = @sql + 'sum(case 病因 when '''+病因+''' then 病人数 else 0 end) as '''+病因+''','
from (select distinct 病因 from table2) as a
select @sql = left(@sql,len(@sql)-1) + ' from table2 group by 地区)B on A.地区=B.地区'
exec(@sql)
go

drop table table1,table2
子陌红尘 2006-08-30
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 地区'

select
@sql=@sql+','+病因+'=sum(case 病因 when '''+病因+''' then 病人数 else 0 end)'
from
Table2
group by
病因

set @sql='select b.*,a.人口总数 from Table1 a,('+@sql+' from Table2 group by 地区) b where a.地区=b.地区'

print @sql

exec(@sql)
子陌红尘 2006-08-30
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 地区'
select @sql=@sql+','+病因+'=sum(case 病因 when '''+病因+''' then 病人数 else 0 end)'
from Table2 group by 病因
set @sql=@sql+',人口总数=sum(病人数) from Table2 group by 地区'
exec(@sql)
九斤半 2006-08-30
  • 打赏
  • 举报
回复
-- ======================================================
-- 交叉表实例
-- 基于SQL SERVER 2000
-- ======================================================

-- 在查询分析器里运行:
CREATE TABLE [Test](
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Source] [numeric](18, 0) NULL
) ON [PRIMARY]
GO

INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)
Go

-- 用于:交叉表的列数是确定的
select name,sum(case subject when '数学' then source else 0 end) as '数学',
sum(case subject when '英语' then source else 0 end) as '英语',
sum(case subject when '语文' then source else 0 end) as '语文'
from test
group by name


--用于:交叉表的列数是不确定的
declare @sql varchar(8000)

SELECT @sql = 'select name,'
select @sql = @sql + 'sum(case subject when '''+subject+''' then source else 0 end) as '''+subject+''','
from (select distinct subject from test) as a
select @sql = left(@sql,len(@sql)-1) + ' from test group by name'
exec(@sql)
go

DROP TABLE TEST
GO

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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