有一个表
TBL01,字段如下
ID PayInfo
========================
1 学费:100|杂费:20
2 学费:100|书本费:10
3 学费:100|杂费:20|保险费:10
其中学费,杂费,书本费,保险费是由另一张表得到的
PayItems
ID MoneyName
===================
1 学费
2 杂费
3 书本费
4 保险费
因为历史原因结构已经不能动了,现在我想将PayInfo扩展为一个视图
View01,实现的效果如下
ID 学费 杂费 书本费 保险费
===================================================
1 100 20 0 0
2 100 0 10 0
3 100 20 0 10
请为这个视图应该如何创建?
------解决方案--------------------
第二个表貌似没必要用到哦
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-04-15 22:05:52
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
-- Jun 17 2011 00:57:23
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[TBL01]
if object_id('[TBL01]') is not null drop table [TBL01]
go
create table [TBL01]([ID] int,[PayInfo] varchar(26))
insert [TBL01]
select 1,'学费:100
------解决方案--------------------
杂费:20' union all
select 2,'学费:100
------解决方案--------------------
书本费:10' union all
select 3,'学费:100
------解决方案--------------------
杂费:20
------解决方案--------------------
保险费:10'
--> 测试数据:[PayItems]
if object_id('[PayItems]') is not null drop table [PayItems]
go
create table [PayItems]([ID] int,[MoneyName] varchar(6))
insert [PayItems]
select 1,'学费' union all
select 2,'杂费' union all
select 3,'书本费' union all
select 4,'保险费'
--------------开始查询--------------------------
SELECT id,[学费]=SUM(CASE WHEN payInfo='学费' THEN CONVERT(INT,[money]) ELSE 0 END ),