Here is my issue: I have got a table where are stored images for an estate. So far I created a view (see below) retrieving the 1st image for the estateNow I need to alter thie view in order to retrieve the 1st image unless an image has been preselected (Prf_UseThisOne=1) Here is the tableCREATE TABLE [dbo].[tblPropImages]( [Prf_Id] [int] IDENTITY(1,1) NOT NULL, [Prf_Pr_Id] [int] NOT NULL, [Prf_Image] [nvarchar](50) NOT NULL, [Prf_Position] [int] NOT NULL, [Prf_UseThisOne] [int] NOT NULL, CONSTRAINT [PK_tblPropImages] PRIMARY KEY CLUSTERED ( [Prf_Id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [dbo].[tblPropImages] ADD CONSTRAINT [DF_Table_1_Prf_Posicion] DEFAULT ((0)) FOR [Prf_Position]GOALTER TABLE [dbo].[tblPropImages] ADD CONSTRAINT [DF_Table_1_Prf_Primero] DEFAULT ((0)) FOR [Prf_UseThisOne]GO
Here are the dataINSERT INTO [dbo].[tblPropImages] ([Prf_Pr_Id] ,[Prf_Image] ,[Prf_Position] ,[Prf_UseThisOne]) VALUES (1,'img1',5,0)GOINSERT INTO [dbo].[tblPropImages] ([Prf_Pr_Id] ,[Prf_Image] ,[Prf_Position] ,[Prf_UseThisOne]) VALUES (1,'img2',15,0)GOINSERT INTO [dbo].[tblPropImages] ([Prf_Pr_Id] ,[Prf_Image] ,[Prf_Position] ,[Prf_UseThisOne]) VALUES (1,'img3',10,0)GOINSERT INTO [dbo].[tblPropImages] ([Prf_Pr_Id] ,[Prf_Image] ,[Prf_Position] ,[Prf_UseThisOne]) VALUES (2,'img10',5,0)GOINSERT INTO [dbo].[tblPropImages] ([Prf_Pr_Id] ,[Prf_Image] ,[Prf_Position] ,[Prf_UseThisOne]) VALUES (2,'img20',15,1)GOINSERT INTO [dbo].[tblPropImages] ([Prf_Pr_Id] ,[Prf_Image] ,[Prf_Position] ,[Prf_UseThisOne]) VALUES (2,'img30',10,0)GO
Here is the viewCREATE VIEW [dbo].[vwImage]ASSELECT B.Prf_Id, B.Prf_Pr_Id, B.Prf_Image, B.Prf_PositionFROM dbo.tblPropImages AS B INNER JOIN (SELECT TOP (100) PERCENT Prf_Pr_Id, MIN(Prf_Position) AS iPosition FROM dbo.tblPropImages GROUP BY Prf_Pr_Id) AS FO ON FO.Prf_Pr_Id = B.Prf_Pr_Id AND FO.iPosition = B.Prf_PositionGO
This view gives the following resultPrf_Id Prf_Pr_Id Prf_Image Prf_Position1 1 img1 54 2 img10 5But now I need to alter this view in order to getPrf_Id Prf_Pr_Id Prf_Image Prf_Position1 1 img1 55 2 img20 15jean-lucwww.corobori.com