我想提出一个观点

时间:2011-11-02 18:07:40

标签: sql-server

这是我的表

CREATE TABLE [dbo].[StandardRoom](
[StandardRoomId] [char](4) NOT NULL,
[RoomType] [varchar](15) NULL,
[Rate] [smallmoney] NULL,
[RoomDeparment] [varchar](15) NULL,
[PlaceNb] [char](1) NULL,
[PatientId1] [char](12) NULL,
[PatientId2] [char](12) NULL,
[PatientId3] [char](12) NULL,
[PatientId4] [char](12) NULL,
[BedId1] [char](4) NOT NULL,
[BedId2] [char](4) NULL,
[BedId3] [char](4) NULL,
[BedId4] [char](4) NULL,
[Full] [char](1) NOT NULL,

所以我想创建一个视图来看(如果PatientId1 = null,则为BedId1),(如果PatientId2 = null,则为BedId2) (如果PatientId3 = null,则为BedId3),(如果PatientId4 = null,则为BedId4)

感谢帮助我!

对于那些投票的人,因为他们认为这张桌子没有正常化,是的,这是真的,但我有理由在我的数据库的背景下这样做...

1 个答案:

答案 0 :(得分:2)

CREATE VIEW [dbo].[v_StandardRoom]
AS
select [StandardRoomId] ,
[RoomType] ,
[Rate] ,
[RoomDeparment] ,
[PlaceNb] ,
[PatientId1] ,
[PatientId2] ,
[PatientId3] ,
[PatientId4] ,
case when PatientId1 IS NULL then [BedId1] 
when PatientId2 IS NULL then [BedId2] 
when PatientId3 IS NULL then [BedId3] 
when PatientId4 IS NULL then [BedId4] 
end as BedId,
[Full] 
from [dbo].[v_StandardRoom]