表1
  科室编码            费用名称            金额
     1                手术费              120
     1                检查费              80
     2                药费                230
     2                B超费               180
    ...               ....                ...
     .                  .                  .
     .                  .                  .
    ...               ....                ...
表2
  科室编码            科室名称
     1                外科
     2                内科
    ...              .....
     .                 .
     .                 .
     N                 放射科要求输出表3
   科室名称           手术费      检查费     药费      B超    .......
    外科              120          80        0        0      ......
    内科               0           0         230     180     ......
    ....              ...         ...        ...     ...     ......
请CSDN上的兄弟姐妹支持

解决方案 »

  1.   

    你用的SQL Server吗,可以用case语句写出来
      

  2.   

    我用的是sql server,请randy_mic(生于70年代)赐教
      

  3.   

    select a.科室名称,(select isNULL(金额,0) from b where a.科室编号=b.科室编号 and b.药费名称='手术费'),(select isNULL(金额,0) from b where a.科室编号=b.科室编号 and b.药费名称='检查费'),(select isNULL(金额,0) from b where a.科室编号=b.科室编号 and b.药费名称='药费'),(select isNULL(金额,0) from b where a.科室编号=b.科室编号 and b.药费名称='B超')) from 表1 a,表2 b
    这是SQL Server可以实现的语句。
      

  4.   

    http://expert.csdn.net/Expert/topic/1366/1366777.xml?temp=.1545069 看看
      

  5.   

    select b.科室名称,
    sum(case a.费用名称 when a.手术费 then a.金额 else 0),
    sum(case a.费用名称 when a.检查费 then a.金额 else 0),
    sum(case a.费用名称 when a.药费 then a.金额 else 0),
    ................
    from 表1 a,表2 b
    where a.科室编码=b.科室编码
    group by a.科室编码
      

  6.   

    to shaperock(单刀侠):谢谢您的关注,sorry,我还有一点没讲清楚,那就是表1中的费用名称不是固定的,因为表1是临时表,好比说B超费有时有,有时又没有,其它费用名称也类似。科室对应的费用名称也不固定。还请shaperock(单刀侠)继续关注,不胜感激!!!
      

  7.   

    to wjlsmail(计算机质子):先谢谢您的关注,改表结构的可能性不大,因为表1是我在存储过程中创建的临时表,它是由SQL语句插入的数据,请继续关注好吗?!
      

  8.   

    to bluemeteor(挂月):谢谢您的关注,sorry,我还有一点没讲清楚,那就是表1中的费用名称不是固定的,因为表1是临时表,好比说B超费有时有,有时又没有,其它费用名称也类似。科室对应的费用名称也不固定。用Case语句应该写不出来,还请bluemeteor(挂月)继续关注,不胜感激!!!
      

  9.   

    你的问题应该这样写:select b.科室名称, 
    (case 费用名称 when 手术费 then 金额 else 0 end) 手术费, 
    (case 费用名称 when 检查费 then 金额 else 0 end) 检查费,
    (case 费用名称 when 药费 then 金额 else 0 end) 药费,
    (case 费用名称 when B超 then 金额 else 0 end) B超,
    from 表1 a, 表2 b where a.科室编码=b.科室编码 
    group by b.科室名称,手术费,检查费,药费,B超
      

  10.   

    你的问题应该这样写:select b.科室名称, 
    (case a.费用名称 when 手术费 then a.金额 else 0 end) 手术费, 
    (case a.费用名称 when 检查费 then a.金额 else 0 end) 检查费,
    (case a.费用名称 when 药费 then a.金额 else 0 end) 药费,
    (case a.费用名称 when B超 then a.金额 else 0 end) B超,
    from 表1 a, 表2 b where a.科室编码=b.科室编码 
    group by b.科室名称,手术费,检查费,药费,B超
      

  11.   

    请大家再顶一下,晚点结贴,我看
    http://expert.csdn.net/Expert/topic/1366/1366777.xml?temp=.1545069 上XRS(心如水)这样的问题已结贴,不知他是怎样实现的?我按上面各位大侠的高见去做了,还是实现不了,有谁知道XRS(心如水)怎么做的吗?恳请联系XRS(心如水).
      

  12.   

    http://expert.csdn.net/Expert/topic/1529/1529397.xml?temp=.6069757,看看大力的,它得也许对。
      

  13.   

    结贴,敬请ShapeRock(单刀侠)、pengdail(大力)接份,万分感激您们及关注者
      

  14.   

    以下是个人意见,希望大家指正!楼主的问题,很常见,
    我使用过2种方法处理过:
    1、前台(或者说客户端)处理 (第1种方法,我做过控件处理显示 预览 打印)
    2、后台(数据库存储过程)处理(使用临时表辅助处理)
    第2种方法(sql语句怎么写):
    表1:(科室编码 ,费用名称 ,金额)
    表2:(科室编码 ,科室名称)1:create table #Temp (No int , Name varchar(30))2:insert into #Temp  (No,Name)
       select distinct 科室编码 ,科室名称
       from 表23:declare @Money char(20)
       declare @iCount char(20)
       declare @FieldName char(21)
       set @iCount = 1   declare MyMoney cursor for 
       select distinct 费用名称 
       from 表1   open MyMoney
       fetch MyMoney into @Money
       while @@fetch_status = 0 
       begin
          set @FieldName = 'Field'+Rtrim(Convert(char(3),@iCount))
          Exec(N'alter table #Temp ADD ['+@FieldName+'] float ')      
          
          Exec(N'update #Temp set ['+@FieldName+']='+
                'isnull(表1.金额,0) from 表1 where #Temp.No =表1.科室编码 and '+
                '表1.费用名称 = '+'"'@Money+'"')      
                fetch MyMoney into @Money
          set @iCount = @iCount +1
       end
       close MyMoney
       deallocate MyMoney4:select * from #Temp
       order by No最终显示结果:
    No      Name      Field1    Field2    Field3    Field4 ....
    ====    ========  ========  ========  ========  ========
    还需要作的事情,把英文的字段名,转换成中文