34,576
社区成员
发帖
与我相关
我的任务
分享
create table emi
(诊断 varchar(16), 费用类别 int, 费用金额 decimal(5,2))
insert into emi
select '白内障', 1, 30.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 90.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 90.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 90.00 union all
select '白内障', 1, 105.00 union all
select '白内障', 2, 12.00 union all
select '白内障', 2, 12.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 8.00 union all
select '白内障', 2, 8.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 14.00 union all
select '白内障', 3, 36.00 union all
select '白内障', 3, 122.00 union all
select '白内障', 3, 120.00 union all
select '白内障', 3, 120.00 union all
select '白内障', 3, 38.00 union all
select '白内障', 3, 35.00 union all
select '白内障', 3, 35.00 union all
select '白内障', 3, 37.00 union all
select '白内障', 3, 35.00 union all
select '白内障', 3, 121.00 union all
select '白内障', 3, 120.00 union all
select '白内障', 4, 105.50 union all
select '膀胱恶性肿瘤', 1, 30.00 union all
select '膀胱恶性肿瘤', 1, 150.00 union all
select '膀胱恶性肿瘤', 1, 330.00 union all
select '膀胱恶性肿瘤', 1, 60.00 union all
select '膀胱恶性肿瘤', 1, 120.00 union all
select '膀胱恶性肿瘤', 2, 16.00 union all
select '膀胱恶性肿瘤', 2, 16.00 union all
select '膀胱恶性肿瘤', 2, 28.00 union all
select '膀胱恶性肿瘤', 2, 18.00 union all
select '膀胱恶性肿瘤', 2, 4.00 union all
select '膀胱恶性肿瘤', 3, 25.00 union all
select '膀胱恶性肿瘤', 3, 334.00 union all
select '膀胱恶性肿瘤', 4, 152.00 union all
select '膀胱恶性肿瘤', 4, 49.00 union all
select '膀胱恶性肿瘤', 4, 29.00 union all
select '膀胱恶性肿瘤', 4, 29.00 union all
select '膀胱恶性肿瘤', 4, 313.50 union all
select '膀胱恶性肿瘤', 5, 277.00
--费用类别固定
select
[诊断]
, sum(case when [费用类别]=1 then [费用金额] else 0 end) as [费用类别1]
, sum(case when [费用类别]=2 then [费用金额] else 0 end) as [费用类别2]
, sum(case when [费用类别]=3 then [费用金额] else 0 end) as [费用类别3]
, sum(case when [费用类别]=4 then [费用金额] else 0 end) as [费用类别4]
, sum(case when [费用类别]=5 then [费用金额] else 0 end) as [费用类别5]
from emi
group by [诊断]
--费用类别不固定
declare @sql varchar(max)
set @sql='select [诊断]'
select @sql=@sql+' ,sum(case [费用类别] when '+ convert(varchar(20),费用类别) + ' then 费用金额 else 0 end) as[费用类别'+convert(varchar(20),费用类别)+']'
from (select distinct [费用类别] from emi) as a
set @sql=@sql + ' from emi group by [诊断]'
exec (@sql)
--结果:
诊断 费用类别1 费用类别2 费用类别3 费用类别4 费用类别5
---------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
白内障 855.00 114.00 819.00 105.50 0.00
膀胱恶性肿瘤 690.00 82.00 359.00 572.50 277.00
(2 行受影响)
with emi as
(select c.ZD 诊断,a.LB 费用类别,a.HZJE 费用金额
from SRCXB_ZYBR a,NHBBD b ,CYZMS c
where a.ZYH=b.ZYH and a.ZYH=c.ZYH
)
select 诊断,
isnull([1],0) '类别1金额合计',
isnull([2],0) '类别2金额合计',
isnull([3],0) '类别3金额合计',
isnull([4],0) '类别4金额合计',
isnull([5],0) '类别5金额合计'
from emi
pivot(sum(费用金额) for 费用类别 in([1],[2],[3],[4],[5])) t
if exists(select * from sysobjects where [name]='tb')
drop table tb
create table [tb]([ZD] varchar(20),[LB] varchar(4),[HZJE] money)
go
insert tb
select
'白内障','1','30.00'
union select
'白内障','1','75.00'
union select
'白内障','1','90.00'
union select
'白内障','1','75.00'
union select
'白内障','1','75.00'
union select
'白内障','1','75.00'
union select
'白内障','1','90.00'
union select
'白内障','1','75.00'
union select
'白内障','1','75.00'
union select
'白内障','1','90.00'
union select
'白内障','1','105.00'
union select
'白内障','2','12.00'
union select
'白内障','2','12.00'
union select
'白内障','2','10.00'
union select
'白内障','2','10.00'
union select
'白内障','2','10.00'
union select
'白内障','2','8.00'
union select
'白内障','2','8.00'
union select
'白内障','2','10.00'
union select
'白内障','2','10.00'
union select
'白内障','2','10.00'
union select
'白内障','2','14.00'
union select
'白内障','3','36.00'
union select
'白内障','3','122.00'
union select
'白内障','3','120.00'
union select
'白内障','3','120.00'
union select
'白内障','3','38.00'
union select
'白内障','3','35.00'
union select
'白内障','3','35.00'
union select
'白内障','3','37.00'
union select
'白内障','3','35.00'
union select
'白内障','3','121.00'
union select
'白内障','3','120.00'
union select
'白内障','4','105.50'
union select
'膀胱恶性肿瘤','1','30.00'
union select
'膀胱恶性肿瘤','1','150.00'
union select
'膀胱恶性肿瘤','1','330.00'
union select
'膀胱恶性肿瘤','1','60.00'
union select
'膀胱恶性肿瘤','1','120.00'
union select
'膀胱恶性肿瘤','1','16.00'
union select
'膀胱恶性肿瘤','1','16.00'
union select
'膀胱恶性肿瘤','2','28.00'
union select
'膀胱恶性肿瘤','2','18.00'
union select
'膀胱恶性肿瘤','2','4.00'
union select
'膀胱恶性肿瘤','3','25.00'
union select
'膀胱恶性肿瘤','3','334.00'
union select
'膀胱恶性肿瘤','4','152.00'
union select
'膀胱恶性肿瘤','4','49.00'
union select
'膀胱恶性肿瘤','4','29.00'
union select
'膀胱恶性肿瘤','4','29.00'
union select
'膀胱恶性肿瘤','4','313.50'
union select
'膀胱恶性肿瘤','5','277.00';
--select * from tb;
/*
诊断 类别1金额合计 类别2金额合计 类别3金额合计
白内障
膀胱恶性肿瘤
*/
with a as(
select zd ,lb,SUM(hzje)hj from tb
group by ZD,LB)
,b as(
select ZD
,case when lb=1 then hj else 0 end lb_1
,case when lb=2 then hj else 0 end lb_2
,case when lb=3 then hj else 0 end lb_3
,case when lb=4 then hj else 0 end lb_4
,case when lb=5 then hj else 0 end lb_5
from a)
select ZD,SUM(lb_1)类别1金额合计,SUM(lb_2)类别2金额合计,SUM(lb_3)类别3金额合计,SUM(lb_4)类别4金额合计,SUM(lb_5)类别5金额合计
from b group by ZD
create table emi
(诊断 varchar(16), 费用类别 int, 费用金额 decimal(5,2))
insert into emi
select '白内障', 1, 30.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 90.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 90.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 90.00 union all
select '白内障', 1, 105.00 union all
select '白内障', 2, 12.00 union all
select '白内障', 2, 12.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 8.00 union all
select '白内障', 2, 8.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 14.00 union all
select '白内障', 3, 36.00 union all
select '白内障', 3, 122.00 union all
select '白内障', 3, 120.00 union all
select '白内障', 3, 120.00 union all
select '白内障', 3, 38.00 union all
select '白内障', 3, 35.00 union all
select '白内障', 3, 35.00 union all
select '白内障', 3, 37.00 union all
select '白内障', 3, 35.00 union all
select '白内障', 3, 121.00 union all
select '白内障', 3, 120.00 union all
select '白内障', 4, 105.50 union all
select '膀胱恶性肿瘤', 1, 30.00 union all
select '膀胱恶性肿瘤', 1, 150.00 union all
select '膀胱恶性肿瘤', 1, 330.00 union all
select '膀胱恶性肿瘤', 1, 60.00 union all
select '膀胱恶性肿瘤', 1, 120.00 union all
select '膀胱恶性肿瘤', 2, 16.00 union all
select '膀胱恶性肿瘤', 2, 16.00 union all
select '膀胱恶性肿瘤', 2, 28.00 union all
select '膀胱恶性肿瘤', 2, 18.00 union all
select '膀胱恶性肿瘤', 2, 4.00 union all
select '膀胱恶性肿瘤', 3, 25.00 union all
select '膀胱恶性肿瘤', 3, 334.00 union all
select '膀胱恶性肿瘤', 4, 152.00 union all
select '膀胱恶性肿瘤', 4, 49.00 union all
select '膀胱恶性肿瘤', 4, 29.00 union all
select '膀胱恶性肿瘤', 4, 29.00 union all
select '膀胱恶性肿瘤', 4, 313.50 union all
select '膀胱恶性肿瘤', 5, 277.00
select 诊断,
isnull([1],0) '类别1金额合计',
isnull([2],0) '类别2金额合计',
isnull([3],0) '类别3金额合计',
isnull([4],0) '类别4金额合计',
isnull([5],0) '类别5金额合计'
from emi
pivot(sum(费用金额) for 费用类别 in([1],[2],[3],[4],[5])) t
诊断 类别1金额合计 类别2金额合计 类别3金额合计 类别4金额合计 类别5金额合计
---------------- ------------- -------------- -------------- -------------- ---------------
白内障 855.00 114.00 819.00 105.50 0.00
膀胱恶性肿瘤 690.00 82.00 359.00 572.50 277.00
(2 row(s) affected)
create table emi
(诊断 varchar(16), 费用类别 int, 费用金额 decimal(5,2))
insert into emi
select '白内障', 1, 30.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 90.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 90.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 75.00 union all
select '白内障', 1, 90.00 union all
select '白内障', 1, 105.00 union all
select '白内障', 2, 12.00 union all
select '白内障', 2, 12.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 8.00 union all
select '白内障', 2, 8.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 10.00 union all
select '白内障', 2, 14.00 union all
select '白内障', 3, 36.00 union all
select '白内障', 3, 122.00 union all
select '白内障', 3, 120.00 union all
select '白内障', 3, 120.00 union all
select '白内障', 3, 38.00 union all
select '白内障', 3, 35.00 union all
select '白内障', 3, 35.00 union all
select '白内障', 3, 37.00 union all
select '白内障', 3, 35.00 union all
select '白内障', 3, 121.00 union all
select '白内障', 3, 120.00 union all
select '白内障', 4, 105.50 union all
select '膀胱恶性肿瘤', 1, 30.00 union all
select '膀胱恶性肿瘤', 1, 150.00 union all
select '膀胱恶性肿瘤', 1, 330.00 union all
select '膀胱恶性肿瘤', 1, 60.00 union all
select '膀胱恶性肿瘤', 1, 120.00 union all
select '膀胱恶性肿瘤', 2, 16.00 union all
select '膀胱恶性肿瘤', 2, 16.00 union all
select '膀胱恶性肿瘤', 2, 28.00 union all
select '膀胱恶性肿瘤', 2, 18.00 union all
select '膀胱恶性肿瘤', 2, 4.00 union all
select '膀胱恶性肿瘤', 3, 25.00 union all
select '膀胱恶性肿瘤', 3, 334.00 union all
select '膀胱恶性肿瘤', 4, 152.00 union all
select '膀胱恶性肿瘤', 4, 49.00 union all
select '膀胱恶性肿瘤', 4, 29.00 union all
select '膀胱恶性肿瘤', 4, 29.00 union all
select '膀胱恶性肿瘤', 4, 313.50 union all
select '膀胱恶性肿瘤', 5, 277.00
DECLARE @SQL VARCHAR(8000),@I INT,@N INT
SELECT @SQL = '',@N = 1, @I=MAX(费用类别) FROM emi
WHILE @N<=@I
BEGIN
SET @SQL = @SQL + ',' + CAST(@N AS VARCHAR) + ' AS [类别 ' + CAST(@N AS VARCHAR) +'],ISNULL((SELECT SUM(费用金额) FROM emi where 诊断=t1.诊断 and 费用类别=''' +CAST(@N as varchar)+ '''),0) AS [金额合计]'
set @N = @N + 1
END
SET @SQL = 'SELECT 诊断 ' + @SQL + ' FROM emi T1 GROUP BY 诊断'
PRINT @SQL
EXEC (@SQL)
drop table emi