数据库 现在有表1,表2如下,要求输出表3的样子Table1
yue dep yj
一月 1 10
二月 1 20
三月 2 30
Table2
dep dep_name
1 部门1
2 部门2
3 部门3
4 部门4
Table3
dep 一月 二月 三月
1
Table3dep 一月 二月 三月1 10 20 null2 null null 303 null null null4 null null null
yue dep yj
一月 1 10
二月 1 20
三月 2 30
Table2
dep dep_name
1 部门1
2 部门2
3 部门3
4 部门4
Table3
dep 一月 二月 三月
1
Table3dep 一月 二月 三月1 10 20 null2 null null 303 null null null4 null null null
解决方案 »
- 请求sql server 英文版下载?
- 在线求SQL题
- 关于取得服务器时间的问题
- 如何优化如下SQL语句?
- 各位老大,给个新建函数的例子吧!
- Hi,请教一个SSIS的问题:看了你的《 SSIS处理导入数据时, 存在的更新, 不存在的插入》如何取得新插入的ID?
- 实在是看不出错在哪里,请各位高手帮帮忙
- 看到绝大多数的金融系统都是有Oracle和Sybase的,几乎没有用MS的。有这方面的专家可以给我提些意见吗?
- 三维表怎么建?要存储每个人领每种物品的时间,并要求人和物品都是可变的!
- 我有一个程序,后台是SQL7,用Ttable,把cachedupdates设置为True,然后用Applyupdate,在一个用户时正常,但如果两人同时提交,两台机的程序都死掉了。急!!!!!
- 大家看看这个数据库建立后,ip uid pwd 应该是什么?
- audit logout占用大量服务器资源,如何解决?
if object_id('tb1')is not null drop table tb1
go
create table tb1(yue varchar(10), dep int, yj int)
insert tb1 select
'一月' , 1, 10 union all select
'二月', 1 , 20 union all select
'三月' , 2 , 30
if object_id('tb2')is not null drop table tb2
go
create table tb2( dep int, dep_name varchar(10))
insert tb2 select
1, '部门1' union all select
2, '部门2'union all select
3, '部门3' union all select
4, '部门4' select b.dep,
[一月]=max(case when yue='一月' then yj else null end),
[二月]=max(case when yue='二月' then yj else null end),
[三月]=max(case when yue='三月' then yj else null end)
from tb2 b
left join tb1 a on a.dep=b.dep
group by b.depdep 一月 二月 三月
----------- ----------- ----------- -----------
1 10 20 NULL
2 NULL NULL 30
3 NULL NULL NULL
4 NULL NULL NULL
警告: 聚合或其他 SET 操作消除了空值。(4 行受影响)
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-10 10:28:42
-- 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.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[Table1]
if object_id('[Table1]') is not null drop table [Table1]
go
create table [Table1]([yue] varchar(4),[dep] int,[yj] int)
insert [Table1]
select '一月',1,10 union all
select '二月',1,20 union all
select '三月',2,30
--> 测试数据:[Table2]
if object_id('[Table2]') is not null drop table [Table2]
go
create table [Table2]([dep] int,[dep_name] varchar(5))
insert [Table2]
select 1,'部门1' union all
select 2,'部门2' union all
select 3,'部门3' union all
select 4,'部门4'
--------------开始查询--------------------------
;with f as
(
select b.[dep],a.[yue],a.[yj] from [Table2] b left join [Table1] a on a.dep=b.dep
)
select distinct dep as dep ,
max(case yue when '一月' then yj else 0 end) 一月,
max(case yue when '二月' then yj else 0 end) 二月,
max(case yue when '三月' then yj else 0 end) 三月
from f
group by dep
----------------结果----------------------------
/* dep 一月 二月 三月
----------- ----------- ----------- -----------
1 10 20 0
2 0 0 30
3 0 0 0
4 0 0 0(4 行受影响)
*/
select dep,[一月],[二月],[三月]
from (select yue,dep,yj from tab union all
select '一月',3,null union all
select '一月',4,null) p
pivot (sum(yj) for yue in ([一月],[二月],[三月])) pvt
declare @Table2 table (dep int,dep_name nvarchar(30))
insert into @Table1
select '一月',1,10 union all
select '二月',1,20 union all
select '三月',2,30
insert into @Table2
select 1,'部门1' union all
select 2,'部门2' union all
select 3,'部门3' union all
select 4,'部门4'
select a.dep,
max(case b.yue when '一月' then yi else null end) as 一月,
max(case b.yue when '二月' then yi else null end) as 二月,
max(case b.yue when '三月' then yi else null end) as 三月
from @Table2 a left join @Table1 b
on a.dep = b.dep
group by a.dep
--结果
/*
1 10.00 20.00 NULL
2 NULL NULL 30.00
3 NULL NULL NULL
4 NULL NULL NULL
*/