create table #tmp(项目 varchar(4),人数 int,金额 int)  
insert into #tmp
select 1,100,40 UNION ALL
select 2,200,40  create table #tmp2(type varchar(10),typename varchar(20))  --字段名称,单位
insert into #tmp2
select '人数','人' union
select '金额','百万'输出结果:项目  类别    单位   数量
 1    人数    人     100
 2    人数    人     200
 1    金额   百万    40
 2    金额   百万    40

解决方案 »

  1.   

    select a.项目,b.type as 类别,b.typename as 单位,a.人数 as 数量 from #Tmp b,#Tmp2 b where b.type='人数'
    union all
    select a.项目,b.type,b.typename,a.金额 from #Tmp b,#Tmp2 b where b.type='金额'
      

  2.   

    create table #tmp(项目 varchar(4),人数 int,金额 int)  
    insert into #tmp
    select 1,100,40 UNION ALL
    select 2,200,40  create table #tmp2(type varchar(10),typename varchar(20))  --字段名称,单位
    insert into #tmp2
    select '人数','人' union
    select '金额','百万'goselect m.项目 , n.type 类别 ,n.typename  单位 ,m.人数 数量 from #tmp m, #tmp2 n where n.type = '人数'
    union all
    select m.项目 , n.type 类别 ,n.typename  单位 ,m.金额 数量 from #tmp m, #tmp2 n where n.type = '金额'
    drop table #tmp , #tmp2/*
    项目   类别         单位                   数量          
    ---- ---------- -------------------- ----------- 
    1    人数         人                    100
    2    人数         人                    200
    1    金额         百万                   40
    2    金额         百万                   40(所影响的行数为 4 行)
    */
      

  3.   

    create table #tmp(项目 varchar(4),人数 int,金额 int)  
    insert into #tmp
    select 1,100,40 UNION ALL
    select 2,200,40  create table #tmp2(type varchar(10),typename varchar(20))  --字段名称,单位
    insert into #tmp2
    select '人数','人' union
    select '金额','百万'
    select 项目 , type as 类别 ,   typename as 单位   ,数量= case typename when '人' then 人数 else  金额 end
    from #tmp2,#tmp order by type desc
    /*
    项目   类别         单位                   数量
    ---- ---------- -------------------- -----------
    1    人数         人                    100
    2    人数         人                    200
    1    金额         百万                   40
    2    金额         百万                   40(4 行受影响)
    */
      

  4.   


    如果#tmp表中还有很多的内容,比如:加工费,直接费用,间接费用,........有40项.我总不能一个一个UNION ALL
      

  5.   

    create table #tmp(项目 varchar(4),人数 int,金额 int)  
    insert into #tmp
    select 1,100,40 UNION ALL
    select 2,200,40  create table #tmp2(type varchar(10),typename varchar(20))  --字段名称,单位
    insert into #tmp2
    select '人数','人' union
    select '金额','百万'select p1.项目,P2.type 类别,P2.typename 单位,P1.人数 as 数量 from #tmp P1,#tmp2 P2
    where type='人数'
    union all
    select p1.项目,P2.type 类别,P2.typename 单位,P1.金额 as 数量 from #tmp P1,#tmp2 P2
    where type='金额'项目   类别         单位                   数量
    ---- ---------- -------------------- -----------
    1    人数         人                    100
    2    人数         人                    200
    1    金额         百万                   40
    2    金额         百万                   40(4 行受影响)
      

  6.   

    如果#tmp表中还有很多的内容,比如:加工费,直接费用,间接费用,........有40项.我总不能一个一个CASE吧!
      

  7.   

    设计的问题,你可以用动态调用 syscolumns表显示
      

  8.   

    SELECT 项目,Type 类别,typename 单位,case when type='人数' then 人数 else 金额 end FROM #tmp,#tmp2
      

  9.   

    select p1.项目,P2.type 类别,P2.typename 单位,P1.人数 as 数量 from #tmp P1,#tmp2 P2
    where type='人数'
    union all
    select p1.项目,P2.type 类别,P2.typename 单位,P1.金额 as 数量 from #tmp P1,#tmp2 P2
    where type='金额'
      

  10.   

    2楼写错一个地方:
    select a.项目,b.type as 类别,b.typename as 单位,a.人数 as 数量 
    from #Tmp a,#Tmp2 b where b.type='人数'
    union all
    select a.项目,b.type,b.typename,a.金额 
    from #Tmp a,#Tmp2 b where b.type='金额'
      

  11.   


    create table #tmp(项目 varchar(4),人数 int,金额 int)  
    insert into #tmp
    select 1,100,40 UNION ALL
    select 2,200,40  create table #tmp2(type varchar(10),typename varchar(20))  --字段名称,单位
    insert into #tmp2
    select '人数','人' union
    select '金额','百万'select * from  #tmp m, #tmp2  
    直接就可以哦。笛卡尔
      

  12.   

    --稍有修改字段
    /*
    create table #tmp(item_no varchar(4),Mtr_Amt int,Amount1 int)  
    insert into #tmp
    select 1,100,40 UNION ALL
    select 2,200,40  create table #tmp2(type varchar(10),typename varchar(20))  --字段名称,单位
    insert into #tmp2
    select '人数','人' union
    select '金额','百万'输出结果:项目  类别    单位   数量
     1    人数    人     100
     2    人数    人     200
     1    金额   百万    40
     2    金额   百万    40
    */
    SELECT a.item_no as [项目],b.type as [类别],b.typename as [单位],
    CASE WHEN b.type='人数' THEN a.Mtr_Amt ELSE a.Amount1 END as [数量]
    FROM #Tmp a cross join #tmp2 b 
    ORDER BY b.type desc
      

  13.   

    select a.项目,b.type as 类别,b.typename as 单位,a.人数 as 数量 from #Tmp a,#Tmp2 b where b.type='人数'
    union all
    select a.项目,b.type,b.typename,a.金额 from #Tmp a,#Tmp2 b where b.type='金额'