IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[pal_SurfaceGridAggregateType]')
 AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
	CREATE TABLE [dbo].[pal_SurfaceGridAggregateType](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[aggregateType] [varchar](50) NULL,
		[isActive] [bit] NULL,
	 CONSTRAINT [PK_pal_SurfaceFieldAggregateType] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
END

TRUNCATE TABLE pal_SurfaceGridAggregateType

INSERT INTO pal_SurfaceGridAggregateType
SELECT	'sum',1

INSERT INTO pal_SurfaceGridAggregateType
SELECT	'average',1

IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[pal_SurfaceGridAggregate]')
 AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
	CREATE TABLE [dbo].[pal_SurfaceGridAggregate](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[surfaceId] [int] NULL,
		[surfaceFieldId] [int] NULL,
		[aggregateTypeId] [int] NULL,
	 CONSTRAINT [PK_pal_SurfaceGridAggregate] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
END


IF NOT EXISTS(
    SELECT *
    FROM sys.columns 
    WHERE Name      = N'showAggregate'
      AND Object_ID = Object_ID(N'pal_SurfaceGridOptions'))
BEGIN
    ALTER TABLE pal_SurfaceGridOptions
	ADD showAggregate BIT null; 
END

IF NOT EXISTS(
    SELECT *
    FROM sys.columns 
    WHERE Name      = N'aggregateTitle'
      AND Object_ID = Object_ID(N'pal_SurfaceGridOptions'))
BEGIN
    ALTER TABLE pal_SurfaceGridOptions
	ADD aggregateTitle VARCHAR(50) null; 
END