--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([a] nvarchar(25),[b] nvarchar(29),[c] nvarchar(28),[d] nvarchar(29),[e] nvarchar(26),[f] nvarchar(29),[g] nvarchar(28),[h] nvarchar(24),[i] nvarchar(24))
Insert #T
select N'科室\大类',N'西药费',N'中成药费',N'中草药费',N'卫生材料费',N'中药饮片',N'院内制剂',N'无',N'无' union all
select N'放射科',N'117.42',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00' union all
select N'风湿科',N'125354.50',N'56513.34',N'154381.42',N'891.61',N'152991.38',N'10578.90',N'0.00',N'0.00'
Go
--测试数据结束
DECLARE @str NVARCHAR(MAX);
WITH cte
AS ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY ( SELECT 0
) ) AS num
FROM #T
)
SELECT @str = ';WITH cte AS (
SELECT *,ROW_NUMBER()OVER(ORDER BY(SELECT 0)) AS num FROM #T
) select a as [' + a + '], b as [' + b + '], c as [' + c + '], d as [' + d
+ '], e as [' + e + '], f as [' + f + '], g as [' + g
+ '], h as [' + h + '], g as [' + i + ']'
FROM cte a
WHERE a.num = 1
SET @str += ' from cte b where b.num<>1'
EXEC(@str)
select @bt='科室\大类',
@med_01=(select top_name from medtopclass where top_no='01'),
@med_02=(select top_name from medtopclass where top_no='02'),
@med_03=isnull((select top_name from medtopclass where top_no='03'),'无'),
@med_04=isnull((select top_name from medtopclass where top_no='04'),'无'),
@med_05=isnull((select top_name from medtopclass where top_no='05'),'无'),
@med_06=isnull((select top_name from medtopclass where top_no='06'),'无'),
@med_07=isnull((select top_name from medtopclass where top_no='07'),'无'),
@med_08='其它'
set @sql=N'
select z as '''+@bt+''',
isnull(SUM(a),0) as '+@med_01+',isnull(SUM(b),0) as '+@med_02+',isnull(SUM(c),0) as '+@med_03+',isnull(SUM(d),0) as '+@med_04+',
isnull(SUM(e),0) as '+@med_05+',isnull(SUM(f),0) as '+@med_06+',isnull(SUM(g),0) as '+@med_07+',isnull(SUM(h),0) as '+@med_08+'
from (
SELECT dept_name
,case when c.top_no=''01'' then sum(fee_money) end,case when c.top_no=''02'' then sum(fee_money) end
,case when c.top_no=''03'' then sum(fee_money) end,case when c.top_no=''04'' then sum(fee_money) end
,case when c.top_no=''05'' then sum(fee_money) end,case when c.top_no=''06'' then sum(fee_money) end
,case when c.top_no=''07'' then sum(fee_money) end
,case when c.top_no not in (''01'',''02'',''03'',''04'',''05'',''06'',''07'') then sum(fee_money) end
FROM alldiagnandfunc a,department b,medtopclass c
where a.position_id=''0'' and a.cur_date between ''20150317'' and ''20170517'' and
a.medorfee=''0'' and a.diagn_dept=b.dept_no and a.top_no=c.top_no group by dept_name,c.top_no)a(z,a,b,c,d,e,f,g,h)
group by a.z;'
EXEC sp_executesql @sql ;
顺带问一个问题,EXEC sp_executesql @sql这个里面的内容能插入到临时表吗?
#5
可以,类似这样
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([a] nvarchar(25),[b] nvarchar(29),[c] nvarchar(28),[d] nvarchar(29),[e] nvarchar(26),[f] nvarchar(29),[g] nvarchar(28),[h] nvarchar(24),[i] nvarchar(24))
Insert #T
select N'科室\大类',N'西药费',N'中成药费',N'中草药费',N'卫生材料费',N'中药饮片',N'院内制剂',N'无',N'无' union all
select N'放射科',N'117.42',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00' union all
select N'风湿科',N'125354.50',N'56513.34',N'154381.42',N'891.61',N'152991.38',N'10578.90',N'0.00',N'0.00'
if not object_id(N'Tempdb..#TT') is null
drop table #TT
Go
Create table #TT([a] nvarchar(25),[b] nvarchar(29),[c] nvarchar(28),[d] nvarchar(29),[e] nvarchar(26),[f] nvarchar(29),[g] nvarchar(28),[h] nvarchar(24),[i] nvarchar(24))
Go
--测试数据结束
DECLARE @str NVARCHAR(MAX);
WITH cte
AS ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY ( SELECT 0
) ) AS num
FROM #T
)
SELECT @str = ';WITH cte AS (
SELECT *,ROW_NUMBER()OVER(ORDER BY(SELECT 0)) AS num FROM #T
) select a as [' + a + '], b as [' + b + '], c as [' + c + '], d as [' + d
+ '], e as [' + e + '], f as [' + f + '], g as [' + g
+ '], h as [' + h + '], g as [' + i + ']'
FROM cte a
WHERE a.num = 1
SET @str += ' from cte b where b.num<>1'
--插入临时表
INSERT INTO #TT EXEC(@str)
--读取临时表
SELECT * FROM #TT
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([a] nvarchar(25),[b] nvarchar(29),[c] nvarchar(28),[d] nvarchar(29),[e] nvarchar(26),[f] nvarchar(29),[g] nvarchar(28),[h] nvarchar(24),[i] nvarchar(24))
Insert #T
select N'科室\大类',N'西药费',N'中成药费',N'中草药费',N'卫生材料费',N'中药饮片',N'院内制剂',N'无',N'无' union all
select N'放射科',N'117.42',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00' union all
select N'风湿科',N'125354.50',N'56513.34',N'154381.42',N'891.61',N'152991.38',N'10578.90',N'0.00',N'0.00'
Go
--测试数据结束
DECLARE @str NVARCHAR(MAX);
WITH cte
AS ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY ( SELECT 0
) ) AS num
FROM #T
)
SELECT @str = ';WITH cte AS (
SELECT *,ROW_NUMBER()OVER(ORDER BY(SELECT 0)) AS num FROM #T
) select a as [' + a + '], b as [' + b + '], c as [' + c + '], d as [' + d
+ '], e as [' + e + '], f as [' + f + '], g as [' + g
+ '], h as [' + h + '], g as [' + i + ']'
FROM cte a
WHERE a.num = 1
SET @str += ' from cte b where b.num<>1'
EXEC(@str)
#4
用拼接字符串可以:
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([a] nvarchar(25),[b] nvarchar(29),[c] nvarchar(28),[d] nvarchar(29),[e] nvarchar(26),[f] nvarchar(29),[g] nvarchar(28),[h] nvarchar(24),[i] nvarchar(24))
Insert #T
select N'科室\大类',N'西药费',N'中成药费',N'中草药费',N'卫生材料费',N'中药饮片',N'院内制剂',N'无',N'无' union all
select N'放射科',N'117.42',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00' union all
select N'风湿科',N'125354.50',N'56513.34',N'154381.42',N'891.61',N'152991.38',N'10578.90',N'0.00',N'0.00'
Go
--测试数据结束
DECLARE @str NVARCHAR(MAX);
WITH cte
AS ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY ( SELECT 0
) ) AS num
FROM #T
)
SELECT @str = ';WITH cte AS (
SELECT *,ROW_NUMBER()OVER(ORDER BY(SELECT 0)) AS num FROM #T
) select a as [' + a + '], b as [' + b + '], c as [' + c + '], d as [' + d
+ '], e as [' + e + '], f as [' + f + '], g as [' + g
+ '], h as [' + h + '], g as [' + i + ']'
FROM cte a
WHERE a.num = 1
SET @str += ' from cte b where b.num<>1'
EXEC(@str)
select @bt='科室\大类',
@med_01=(select top_name from medtopclass where top_no='01'),
@med_02=(select top_name from medtopclass where top_no='02'),
@med_03=isnull((select top_name from medtopclass where top_no='03'),'无'),
@med_04=isnull((select top_name from medtopclass where top_no='04'),'无'),
@med_05=isnull((select top_name from medtopclass where top_no='05'),'无'),
@med_06=isnull((select top_name from medtopclass where top_no='06'),'无'),
@med_07=isnull((select top_name from medtopclass where top_no='07'),'无'),
@med_08='其它'
set @sql=N'
select z as '''+@bt+''',
isnull(SUM(a),0) as '+@med_01+',isnull(SUM(b),0) as '+@med_02+',isnull(SUM(c),0) as '+@med_03+',isnull(SUM(d),0) as '+@med_04+',
isnull(SUM(e),0) as '+@med_05+',isnull(SUM(f),0) as '+@med_06+',isnull(SUM(g),0) as '+@med_07+',isnull(SUM(h),0) as '+@med_08+'
from (
SELECT dept_name
,case when c.top_no=''01'' then sum(fee_money) end,case when c.top_no=''02'' then sum(fee_money) end
,case when c.top_no=''03'' then sum(fee_money) end,case when c.top_no=''04'' then sum(fee_money) end
,case when c.top_no=''05'' then sum(fee_money) end,case when c.top_no=''06'' then sum(fee_money) end
,case when c.top_no=''07'' then sum(fee_money) end
,case when c.top_no not in (''01'',''02'',''03'',''04'',''05'',''06'',''07'') then sum(fee_money) end
FROM alldiagnandfunc a,department b,medtopclass c
where a.position_id=''0'' and a.cur_date between ''20150317'' and ''20170517'' and
a.medorfee=''0'' and a.diagn_dept=b.dept_no and a.top_no=c.top_no group by dept_name,c.top_no)a(z,a,b,c,d,e,f,g,h)
group by a.z;'
EXEC sp_executesql @sql ;
顺带问一个问题,EXEC sp_executesql @sql这个里面的内容能插入到临时表吗?
#5
顺带问一个问题,EXEC sp_executesql @sql这个里面的内容能插入到临时表吗?
可以,类似这样
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([a] nvarchar(25),[b] nvarchar(29),[c] nvarchar(28),[d] nvarchar(29),[e] nvarchar(26),[f] nvarchar(29),[g] nvarchar(28),[h] nvarchar(24),[i] nvarchar(24))
Insert #T
select N'科室\大类',N'西药费',N'中成药费',N'中草药费',N'卫生材料费',N'中药饮片',N'院内制剂',N'无',N'无' union all
select N'放射科',N'117.42',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00' union all
select N'风湿科',N'125354.50',N'56513.34',N'154381.42',N'891.61',N'152991.38',N'10578.90',N'0.00',N'0.00'
if not object_id(N'Tempdb..#TT') is null
drop table #TT
Go
Create table #TT([a] nvarchar(25),[b] nvarchar(29),[c] nvarchar(28),[d] nvarchar(29),[e] nvarchar(26),[f] nvarchar(29),[g] nvarchar(28),[h] nvarchar(24),[i] nvarchar(24))
Go
--测试数据结束
DECLARE @str NVARCHAR(MAX);
WITH cte
AS ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY ( SELECT 0
) ) AS num
FROM #T
)
SELECT @str = ';WITH cte AS (
SELECT *,ROW_NUMBER()OVER(ORDER BY(SELECT 0)) AS num FROM #T
) select a as [' + a + '], b as [' + b + '], c as [' + c + '], d as [' + d
+ '], e as [' + e + '], f as [' + f + '], g as [' + g
+ '], h as [' + h + '], g as [' + i + ']'
FROM cte a
WHERE a.num = 1
SET @str += ' from cte b where b.num<>1'
--插入临时表
INSERT INTO #TT EXEC(@str)
--读取临时表
SELECT * FROM #TT
#6
顺带问一个问题,EXEC sp_executesql @sql这个里面的内容能插入到临时表吗?
可以,类似这样
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([a] nvarchar(25),[b] nvarchar(29),[c] nvarchar(28),[d] nvarchar(29),[e] nvarchar(26),[f] nvarchar(29),[g] nvarchar(28),[h] nvarchar(24),[i] nvarchar(24))
Insert #T
select N'科室\大类',N'西药费',N'中成药费',N'中草药费',N'卫生材料费',N'中药饮片',N'院内制剂',N'无',N'无' union all
select N'放射科',N'117.42',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00',N'0.00' union all
select N'风湿科',N'125354.50',N'56513.34',N'154381.42',N'891.61',N'152991.38',N'10578.90',N'0.00',N'0.00'
if not object_id(N'Tempdb..#TT') is null
drop table #TT
Go
Create table #TT([a] nvarchar(25),[b] nvarchar(29),[c] nvarchar(28),[d] nvarchar(29),[e] nvarchar(26),[f] nvarchar(29),[g] nvarchar(28),[h] nvarchar(24),[i] nvarchar(24))
Go
--测试数据结束
DECLARE @str NVARCHAR(MAX);
WITH cte
AS ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY ( SELECT 0
) ) AS num
FROM #T
)
SELECT @str = ';WITH cte AS (
SELECT *,ROW_NUMBER()OVER(ORDER BY(SELECT 0)) AS num FROM #T
) select a as [' + a + '], b as [' + b + '], c as [' + c + '], d as [' + d
+ '], e as [' + e + '], f as [' + f + '], g as [' + g
+ '], h as [' + h + '], g as [' + i + ']'
FROM cte a
WHERE a.num = 1
SET @str += ' from cte b where b.num<>1'
--插入临时表
INSERT INTO #TT EXEC(@str)
--读取临时表
SELECT * FROM #TT