数据库 现在有表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

解决方案 »

  1.   


    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 行受影响)
      

  2.   

    ----------------------------------------------------------------
    -- 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 行受影响)
    */
      

  3.   

    晕  写成0了 改成null
      

  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
      

  5.   

    declare @Table1 table (yue nvarchar(10),dep int,yi dec(18,2))
    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
    */