表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 手术费 120
1 检查费 80
2 药费 230
2 B超费 180
... .... ...
. . .
. . .
... .... ...
表2
科室编码 科室名称
1 外科
2 内科
... .....
. .
. .
N 放射科要求输出表3
科室名称 手术费 检查费 药费 B超 .......
外科 120 80 0 0 ......
内科 0 0 230 180 ......
.... ... ... ... ... ......
请CSDN上的兄弟姐妹支持
这是SQL Server可以实现的语句。
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.科室编码
(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超
(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超
http://expert.csdn.net/Expert/topic/1366/1366777.xml?temp=.1545069 上XRS(心如水)这样的问题已结贴,不知他是怎样实现的?我按上面各位大侠的高见去做了,还是实现不了,有谁知道XRS(心如水)怎么做的吗?恳请联系XRS(心如水).
我使用过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 ....
==== ======== ======== ======== ======== ========
还需要作的事情,把英文的字段名,转换成中文