当前位置: 代码迷 >> Sql Server >> 两张表的联合查询,A表的sum值与B表的字段值在一个查询结果中,该怎么解决
  详细解决方案

两张表的联合查询,A表的sum值与B表的字段值在一个查询结果中,该怎么解决

热度:78   发布时间:2016-04-24 09:27:23.0
两张表的联合查询,A表的sum值与B表的字段值在一个查询结果中
表A
   时间 值 项目
2014-01-15 12 测试项目1
2014-01-25 4 测试项目1
2014-02-15 18 测试项目1
2014-02-17 6 测试项目1
2014-03-15 17 测试项目1
2014-04-15 19 测试项目1
2014-05-15 17 测试项目1
2014-06-15 20 测试项目1
2014-07-15 22 测试项目1
2014-08-15 34 测试项目1
2014-09-15 56 测试项目1
2014-10-15 7 测试项目1
2014-11-15 9 测试项目1
2014-12-15 6 测试项目1
--------------------------------------
2015-01-15 12 测试项目2
2015-02-15 18 测试项目2
2015-03-15 17 测试项目2
2015-04-15 19 测试项目2
2015-05-15 17 测试项目2
2015-06-15 20 测试项目2
2015-07-15 22 测试项目2
2015-08-15 34 测试项目2
2015-09-15 56 测试项目2
2015-10-15 7 测试项目2
2015-11-15 9 测试项目2
2015-12-15 6 测试项目2

表B
   年 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月 项目
  2014 30 40 50 32 34 38 37 67 86 67 12 22 测试项目1
  2015 31 41 51 12 24 18 33 63 36 47 62 12 测试项目2
求的查询结果:
时间      A.sum值   B.1月 项目
2014-01  16   30 测试项目1
2014-02  24   40 测试项目1
....
2015-12 6   12 测试项目2

求哪位大神指导一下,万分感谢
------解决思路----------------------
use master
go
if OBJECT_ID('a') is not null drop table a
if OBJECT_ID('b') is not null drop table b
go
create table a(
              时间 datetime,
              值  int,
              项目 varchar(20)
              )
 insert into a
 select '2014-01-15',12,'测试项目1' union all
select '2014-01-25',4,'测试项目1' union all
select '2014-02-15',18,'测试项目1' union all
select '2014-02-17',6,'测试项目1' union all
select '2014-03-15',17,'测试项目1' union all
select '2014-04-15',19,'测试项目1' union all
select '2014-05-15',17,'测试项目1' union all
select '2014-06-15',20,'测试项目1' union all
select '2014-07-15',22,'测试项目1' union all
select '2014-08-15',34,'测试项目1' union all
select '2014-09-15',56,'测试项目1' union all
select '2014-10-15',7,'测试项目1' union all
select '2014-11-15',9,'测试项目1' union all
select '2014-12-15',6,'测试项目1' union all
select '2015-01-15',12,'测试项目2' union all
select '2015-02-15',18,'测试项目2' union all
select '2015-03-15',17,'测试项目2' union all
select '2015-04-15',19,'测试项目2' union all
select '2015-05-15',17,'测试项目2' union all
select '2015-06-15',20,'测试项目2' union all
select '2015-07-15',22,'测试项目2' union all
select '2015-08-15',34,'测试项目2' union all
select '2015-09-15',56,'测试项目2' union all
select '2015-10-15',7,'测试项目2' union all
select '2015-11-15',9,'测试项目2' union all
select '2015-12-15',6,'测试项目2'
go
create table b(
               年 varchar(4),
               [1月] int,
               [2月] int,
               [3月] int,
               [4月] int,
               [5月] int,
               [6月] int,
               [7月] int,
               [8月] int,
               [9月] int,
               [10月] int,
               [11月] int,
               [12月] int,
               项目 varchar(30)
               )
 insert into b
 select 2014,30,40,50,32,34,38,37,67,86,67,12,22,'测试项目1'
union all
select 2015,31,41,51,12,24,18,33,63,36,47,62,12,'测试项目2'
go

select bb.时间,bb.值 as [a.sum],cc.值,cc.项目 from 
(select CONVERT(varchar(7),时间,120) as 时间,项目,SUM(值) as 值 
from a group by CONVERT(varchar(7),时间,120),项目) as bb
left join 
(select left(年+'-'+right('0'+月份,3),7) as 时间,项目,值 
from b unpivot(值 for 月份 in([1月],[2月],[3月],[4月],[5月],[6月],[7月],
[8月],[9月],[10月],[11月],[12月])) as w) as cc
on bb.时间=cc.时间 and bb.项目=cc.项目



/**
时间,a.sum,值,项目
------------------------------------------
2014-01 16 30 测试项目1
2014-02 24 40 测试项目1
2014-03 17 50 测试项目1
2014-04 19 32 测试项目1
2014-05 17 34 测试项目1
2014-06 20 38 测试项目1
2014-07 22 37 测试项目1
**/
  相关解决方案