/* *************************************************************************
 * Copyright 2012-2017 VMware, Inc. All rights reserved. -- VMware Confidential
 * *************************************************************************/

/*==============================================================*/
/* VCDB_views_PostgreSQL.sql                                    */
/* DBMS name:      PostgreSQL Version                           */
/*==============================================================*/

/*==============================================================*/
/* View: VPX_HIST_STAT1                                         */
/*==============================================================*/
DROP VIEW IF EXISTS VPX_HIST_STAT1 CASCADE;

CREATE OR REPLACE VIEW VPX_HIST_STAT1 AS
   SELECT * FROM VPX_HIST_STAT1_1
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_2
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_3
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_4
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_5
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_6
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_7
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_8
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_9
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_10
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_11
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_12
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_13
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_14
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_15
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_16
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_17
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_18
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_19
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_20
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_21
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_22
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_23
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_24
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_25
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_26
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_27
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_28
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_29
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_30
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_31
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_32
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_33
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_34
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_35
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_36
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_37
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_38
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_39
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_40
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_41
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_42
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_43
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_44
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_45
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_46
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_47
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_48
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_49
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_50
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_51
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_52
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_53
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_54
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_55
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_56
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_57
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_58
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_59
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_60
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_61
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_62
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_63
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_64
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_65
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_66
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_67
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_68
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_69
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_70
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_71
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_72
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_73
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_74
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_75
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_76
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_77
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_78
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_79
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_80
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_81
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_82
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_83
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_84
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_85
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_86
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_87
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_88
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_89
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_90
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_91
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_92
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_93
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_94
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_95
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_96
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_97
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_98
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_99
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_100
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_101
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_102
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_103
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_104
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_105
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_106
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_107
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_108
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_109
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_110
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_111
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_112
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_113
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_114
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_115
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_116
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_117
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_118
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_119
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_120
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_121
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_122
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_123
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_124
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_125
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_126
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_127
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_128
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_129
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_130
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_131
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_132
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_133
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_134
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_135
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_136
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_137
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_138
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_139
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_140
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_141
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_142
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_143
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_144
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_145
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_146
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_147
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_148
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_149
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_150
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_151
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_152
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_153
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_154
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_155
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_156
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_157
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_158
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_159
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_160
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_161
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_162
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_163
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_164
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_165
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_166
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_167
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_168
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_169
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_170
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_171
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_172
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_173
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_174
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_175
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_176
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_177
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_178
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_179
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_180
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_181
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_182
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_183
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_184
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_185
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_186
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_187
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_188
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_189
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_190
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_191
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_192
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_193
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_194
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_195
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_196
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_197
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_198
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_199
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_200
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_201
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_202
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_203
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_204
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_205
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_206
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_207
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_208
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_209
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_210
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_211
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_212
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_213
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_214
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_215
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_216
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_217
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_218
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_219
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_220
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_221
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_222
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_223
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_224
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_225
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_226
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_227
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_228
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_229
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_230
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_231
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_232
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_233
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_234
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_235
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_236
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_237
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_238
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_239
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_240
   UNION ALL
   SELECT * FROM VPX_HIST_STAT1_241
;

DROP VIEW IF EXISTS VPX_HIST_STAT2 CASCADE;

CREATE OR REPLACE VIEW VPX_HIST_STAT2 AS
   SELECT * FROM VPX_HIST_STAT2_1
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_2
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_3
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_4
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_5
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_6
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_7
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_8
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_9
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_10
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_11
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_12
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_13
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_14
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_15
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_16
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_17
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_18
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_19
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_20
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_21
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_22
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_23
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_24
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_25
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_26
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_27
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_28
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_29
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_30
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_31
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_32
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_33
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_34
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_35
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_36
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_37
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_38
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_39
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_40
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_41
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_42
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_43
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_44
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_45
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_46
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_47
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_48
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_49
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_50
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_51
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_52
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_53
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_54
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_55
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_56
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_57
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_58
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_59
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_60
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_61
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_62
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_63
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_64
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_65
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_66
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_67
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_68
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_69
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_70
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_71
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_72
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_73
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_74
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_75
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_76
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_77
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_78
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_79
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_80
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_81
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_82
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_83
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_84
   UNION ALL
   SELECT * FROM VPX_HIST_STAT2_85;

DROP VIEW IF EXISTS VPX_HIST_STAT3 CASCADE;

CREATE OR REPLACE VIEW VPX_HIST_STAT3 AS
   SELECT * FROM VPX_HIST_STAT3_1
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_2
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_3
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_4
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_5
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_6
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_7
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_8
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_9
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_10
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_11
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_12
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_13
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_14
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_15
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_16
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_17
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_18
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_19
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_20
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_21
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_22
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_23
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_24
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_25
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_26
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_27
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_28
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_29
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_30
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_31
   UNION ALL
   SELECT * FROM VPX_HIST_STAT3_32;

/*==============================================================*/
/* View: VPX_HIST_STAT4                                         */
/*==============================================================*/
DROP VIEW IF EXISTS VPX_HIST_STAT4 CASCADE;

CREATE OR REPLACE VIEW VPX_HIST_STAT4 AS
   SELECT * FROM VPX_HIST_STAT4_1
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_2
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_3
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_4
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_5
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_6
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_7
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_8
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_9
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_10
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_11
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_12
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_13
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_14
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_15
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_16
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_17
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_18
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_19
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_20
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_21
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_22
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_23
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_24
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_25
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_26
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_27
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_28
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_29
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_30
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_31
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_32
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_33
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_34
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_35
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_36
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_37
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_38
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_39
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_40
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_41
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_42
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_43
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_44
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_45
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_46
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_47
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_48
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_49
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_50
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_51
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_52
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_53
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_54
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_55
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_56
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_57
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_58
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_59
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_60
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_61
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_62
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_63
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_64
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_65
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_66
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_67
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_68
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_69
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_70
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_71
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_72
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_73
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_74
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_75
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_76
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_77
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_78
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_79
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_80
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_81
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_82
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_83
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_84
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_85
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_86
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_87
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_88
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_89
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_90
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_91
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_92
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_93
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_94
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_95
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_96
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_97
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_98
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_99
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_100
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_101
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_102
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_103
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_104
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_105
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_106
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_107
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_108
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_109
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_110
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_111
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_112
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_113
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_114
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_115
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_116
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_117
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_118
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_119
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_120
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_121
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_122
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_123
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_124
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_125
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_126
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_127
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_128
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_129
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_130
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_131
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_132
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_133
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_134
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_135
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_136
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_137
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_138
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_139
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_140
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_141
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_142
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_143
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_144
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_145
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_146
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_147
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_148
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_149
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_150
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_151
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_152
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_153
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_154
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_155
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_156
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_157
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_158
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_159
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_160
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_161
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_162
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_163
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_164
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_165
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_166
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_167
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_168
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_169
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_170
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_171
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_172
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_173
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_174
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_175
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_176
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_177
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_178
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_179
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_180
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_181
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_182
   UNION ALL
   SELECT * FROM VPX_HIST_STAT4_183;

/*==============================================================*/
/* View: VPXV_ENTITY                                            */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_ENTITY;

create or replace view VPXV_ENTITY as
select A.ID,
       A.NAME,
       A.TYPE_ID,
       C.NAME as ENTITY_TYPE,
       A.PARENT_ID,
       B.TYPE_ID as PARENT_TYPE_ID,
       D.NAME as PARENT_ENTITY_TYPE
from (VPX_ENTITY A left outer join VPX_ENTITY B on A.PARENT_ID = B.ID)
		           inner join VPX_OBJECT_TYPE C ON A.TYPE_ID=C.id
				   left outer join VPX_OBJECT_TYPE D ON B.TYPE_ID=D.id
;

/*==============================================================*/
/* View: VPXV_HIST_STAT_DAILY                                   */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_HIST_STAT_DAILY;

create or replace  view VPXV_HIST_STAT_DAILY as
select   st.SAMPLE_TIME
         , COALESCE(sd.FIXED_COLLECTION_INTERVAL,  st.SAMPLE_INTERVAL)
	      as SAMPLE_INTERVAL
	       , sd.ID as STAT_ID
         , sd.NAME as STAT_NAME
         , sd.GROUP_NAME as STAT_GROUP
         , case sd.ROLLUP_TYPE
         when 0 then 'average'
         when 1 then 'maximum'
         when 2 then 'minimum'
         when 3 then 'latest'
         when 4 then 'summation'
         when 5 then 'none'
           end as STAT_ROLLUP_TYPE
         , sc.COUNTER_ID
	 , si.MOID AS ENTITY
	 , d.DEVICE_NAME
	 , sdtd.DEVICE_TYPE_NAME
	 , hs.STAT_VAL as STAT_VALUE
from    VPX_SAMPLE_TIME1 st
        ,VPX_DEVICE d
        left outer join VPX_STAT_DEV_TYPE  sdt  on d.DEVICE_ID=sdt.DEVICE_ID
        left outer join VPX_STAT_DEV_TYPE_DEF sdtd on sdt.DEVICE_TYPE_ID=sdtd.DEVICE_TYPE_ID
        , VPX_STAT_COUNTER sc
        , VPX_STAT_DEF sd
        , VPX_HIST_STAT1 hs
        , VPX_STAT_ID si
where hs.counter_id=sc.COUNTER_ID
and   sc.STAT_ID = sd.ID
and   sc.DEVICE_ID=COALESCE(d.DEVICE_ID,1)
and   hs.TIME_ID = st.TIME_ID
and   sc.ENTITY_ID=si.ID
;

/*==============================================================*/
/* View: VPXV_HIST_STAT_WEEKLY                                  */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_HIST_STAT_WEEKLY;

create or replace  view VPXV_HIST_STAT_WEEKLY as
select  st.SAMPLE_TIME
        , COALESCE(sd.FIXED_COLLECTION_INTERVAL::numeric,  st.SAMPLE_INTERVAL)
	      as SAMPLE_INTERVAL
	      , sd.ID as STAT_ID
        , sd.NAME as STAT_NAME
        , sd.GROUP_NAME as STAT_GROUP
        , case sd.ROLLUP_TYPE
         when 0 then 'average'
         when 1 then 'maximum'
         when 2 then 'minimum'
         when 3 then 'latest'
         when 4 then 'summation'
         when 5 then 'none'
          end as STAT_ROLLUP_TYPE
        , sc.COUNTER_ID
        , si.MOID AS ENTITY
        , d.DEVICE_NAME
        , sdtd.DEVICE_TYPE_NAME
        , hs.STAT_VAL as STAT_VALUE
from    VPX_SAMPLE_TIME2 st
        ,VPX_DEVICE d
        left outer join VPX_STAT_DEV_TYPE  sdt  on d.DEVICE_ID=sdt.DEVICE_ID
        left outer join VPX_STAT_DEV_TYPE_DEF sdtd on sdt.DEVICE_TYPE_ID=sdtd.DEVICE_TYPE_ID
        , VPX_STAT_COUNTER sc
        , VPX_STAT_DEF sd
        , VPX_HIST_STAT2 hs
        , VPX_STAT_ID si
where hs.counter_id=sc.COUNTER_ID
and   sc.STAT_ID = sd.ID
and   sc.DEVICE_ID=COALESCE(d.DEVICE_ID,1)
and   hs.TIME_ID = st.TIME_ID
and   sc.ENTITY_ID=si.ID
;

/*==============================================================*/
/* View: VPXV_HIST_STAT_MONTHLY                                 */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_HIST_STAT_MONTHLY;

create or replace  view VPXV_HIST_STAT_MONTHLY as
select  st.SAMPLE_TIME
        , COALESCE(sd.FIXED_COLLECTION_INTERVAL::numeric,  st.SAMPLE_INTERVAL)
	      as SAMPLE_INTERVAL
	      , sd.ID as STAT_ID
        , sd.NAME as STAT_NAME
        , sd.GROUP_NAME as STAT_GROUP
        , case sd.ROLLUP_TYPE
         when 0 then 'average'
         when 1 then 'maximum'
         when 2 then 'minimum'
         when 3 then 'latest'
         when 4 then 'summation'
         when 5 then 'none'
          end as STAT_ROLLUP_TYPE
       , sc.COUNTER_ID
       , si.MOID AS ENTITY
       , d.DEVICE_NAME
       , sdtd.DEVICE_TYPE_NAME
       , hs.STAT_VAL as STAT_VALUE
from    VPX_SAMPLE_TIME3 st
        ,VPX_DEVICE d
        left outer join VPX_STAT_DEV_TYPE  sdt  on d.DEVICE_ID=sdt.DEVICE_ID
        left outer join VPX_STAT_DEV_TYPE_DEF sdtd on sdt.DEVICE_TYPE_ID=sdtd.DEVICE_TYPE_ID
        , VPX_STAT_COUNTER sc
        , VPX_STAT_DEF sd
        , VPX_HIST_STAT3 hs
        , VPX_STAT_ID si
where hs.counter_id=sc.COUNTER_ID
and   sc.STAT_ID = sd.ID
and   sc.DEVICE_ID=COALESCE(d.DEVICE_ID,1)
and   hs.TIME_ID = st.TIME_ID
and   sc.ENTITY_ID=si.ID
;

/*==============================================================*/
/* View: VPXV_HIST_STAT_YEARLY                                  */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_HIST_STAT_YEARLY;

create or replace  view VPXV_HIST_STAT_YEARLY as
select st.SAMPLE_TIME, COALESCE(sd.FIXED_COLLECTION_INTERVAL::numeric,  st.SAMPLE_INTERVAL)
	      as SAMPLE_INTERVAL
	      , sd.ID as STAT_ID
	      , sd.NAME as STAT_NAME,
       sd.GROUP_NAME as STAT_GROUP,
       case sd.ROLLUP_TYPE
         when 0 then 'average'
         when 1 then 'maximum'
         when 2 then 'minimum'
         when 3 then 'latest'
         when 4 then 'summation'
         when 5 then 'none'
       end as STAT_ROLLUP_TYPE
       , sc.COUNTER_ID
       , si.MOID AS ENTITY
       , d.DEVICE_NAME
       , sdtd.DEVICE_TYPE_NAME
       , hs.STAT_VAL as STAT_VALUE
from    VPX_SAMPLE_TIME4 st
        ,VPX_DEVICE d
        left outer join VPX_STAT_DEV_TYPE  sdt  on d.DEVICE_ID=sdt.DEVICE_ID
        left outer join VPX_STAT_DEV_TYPE_DEF sdtd on sdt.DEVICE_TYPE_ID=sdtd.DEVICE_TYPE_ID
        , VPX_STAT_COUNTER sc
        , VPX_STAT_DEF sd
        , VPX_HIST_STAT4 hs
        , VPX_STAT_ID si
where hs.counter_id=sc.COUNTER_ID
and   sc.STAT_ID = sd.ID
and   sc.DEVICE_ID=COALESCE(d.DEVICE_ID,1)
and   hs.TIME_ID = st.TIME_ID
and   sc.ENTITY_ID=si.ID
;


/*==============================================================*/
/* View: VPXV_STAT_COUNTERS                                     */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_STAT_COUNTERS;

create or replace view VPXV_STAT_COUNTERS as
select    sd.ID AS ID
        ,case sd.ROLLUP_TYPE
         when 0 then 'average'
         when 1 then 'maximum'
         when 2 then 'minimum'
         when 3 then 'latest'
         when 4 then 'summation'
         when 5 then 'none'
         end as STAT_ROLLUP
        , sd.NAME
        , sd.GROUP_NAME
        , case sd.TYPE
         when 0 then 'absolute'
         when 1 then 'delta'
         when 2 then 'rare'
         end as TYPE
        , sd.UNIT
        , sd.ASSOCIATE_IDS
        , sd.STAT_LEVEL AS STATS_LEVEL
        , sd.FIXED_COLLECTION_INTERVAL
FROM  VPX_STAT_DEF sd
;

/*==============================================================*/
/* View: VPXV_STAT_HISTORICAL_INTERVALS                         */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_STAT_HISTORICAL_INTERVALS;

create or replace view VPXV_STAT_HISTORICAL_INTERVALS as
select    sid.INTERVAL_SEQ_NUM  as ID
        , sid.INTERVAL_DEF_NAME as NAME
        , sid.INTERVAL_VAL      as SAMPLING_PERIOD
        , sid.INTERVAL_LENGTH   as LENGTH
        , sid.STATS_LEVEL
        , case sid.ROLLUP_ENABLED_FLG
          when 1 then 'Y'
          when 0 then 'N'
          end as ENABLED
FROM  VPX_STAT_INTERVAL_DEF sid
;

/*==============================================================*/
/* View: VPXV_HOST_DATASTORE                                    */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_HOST_DATASTORE;

create or replace view VPXV_HOST_DATASTORE as
select ENTITY_ID as HOST_ID,
       DS_ID, ACCESSIBLE, MOUNT_PATH, MOUNT_ID, MOUNT_MODE, MOUNTED
from VPX_ENTITY, VPX_DS_ASSIGNMENT
where ENTITY_ID = ID
  and TYPE_ID = 1;

/*==============================================================*/
/* View: VPXV_HOST_NETWORK                                      */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_HOST_NETWORK;

create or replace view VPXV_HOST_NETWORK as
select ENTITY_ID as HOST_ID,
       NETWORK_ID
from VPX_ENTITY, VPX_NW_ASSIGNMENT
where ENTITY_ID = ID
  and TYPE_ID = 1;

/*==============================================================*/
/* View: VPXV_VM_DATASTORE                                      */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_VM_DATASTORE;

create or replace view VPXV_VM_DATASTORE as
select ENTITY_ID as VM_ID,
       DS_ID
from VPX_ENTITY, VPX_DS_ASSIGNMENT
where ENTITY_ID = ID
  and TYPE_ID = 0;

/*==============================================================*/
/* View: VPXV_VM_NETWORK                                        */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_VM_NETWORK;

create or replace view VPXV_VM_NETWORK as
select ENTITY_ID as VM_ID,
       NETWORK_ID
from VPX_ENTITY, VPX_NW_ASSIGNMENT
where ENTITY_ID = ID
  and TYPE_ID = 0;

/*==============================================================*/
/* View: VPXV_COMPUTE_RESOURCE_NETWORK                          */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_COMPUTE_RESOURCE_NETWORK;

create or replace view VPXV_COMPUTE_RESOURCE_NETWORK as
select ENTITY_ID as COMPUTE_RESOURCE_ID,
       NETWORK_ID
from VPX_ENTITY, VPX_NW_ASSIGNMENT
where ENTITY_ID = ID
  and TYPE_ID = 2;

/*==============================================================*/
/* View: VPXV_COMPUTE_RESOURCE                                  */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_COMPUTE_RESOURCE;

create or replace view VPXV_COMPUTE_RESOURCE as
select e.ID as RESOURCEPOOLID
, e.NAME as NAME
, e.PARENT_ID
, c.RESOURCE_GROUP_ID
, c.RESOURCE_TYPE
, c.VM_SWAP_PLACEMENT
, c.SPBM_ENABLED
, CASE c.DAS_ENABLED when 0 then 'NO' when 1 then 'YES' end as HAMode
, CASE c.DRS_ENABLED when 0 then 'NO' when 1 then 'YES' end as DRSMode
, c.FAILOVER_LEVEL
, c.STRICT_ADMISSION_CONTROL
, c.DEFAULT_VM_BEHAVIOR
, c.VMOTION_RATE
, c.DPM_ENABLED_FLG
, c.DEFAULT_HOST_BEHAVIOR
, c.DEFAULT_DAS_PRIORITY
, c.POWER_OFF_ON_ISOLATION
, c.EVC_MODE
, c.VMHEALTH_VM_MONITORING
, c.VMHEALTH_FAILURE_INTERVAL
, c.VMHEALTH_MIN_UPTIME
, c.VMHEALTH_MAX_FAILURES
, c.VMHEALTH_MAX_FAILURE_WINDOW
, c.FAILOVER_CPU_RESOURCES
, c.FAILOVER_MEM_RESOURCES
, c.HOST_POWER_ACTION_RATE
, c.DRS_VM_OVERRIDES_ENABLED_FLG
, c.DAS_HOST_MONITORING
, c.DAS_VM_MONITORING
, c.CLUSTER_STATE
from VPX_ENTITY e, VPX_COMPUTE_RESOURCE c
where e.ID = c.ID;

/*==============================================================*/
/* View: VPXV_HOSTS                                             */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_HOSTS;

create or replace view VPXV_HOSTS as
select e.ID as HOSTID
, e.NAME as NAME
, e.PARENT_ID as FARMID
, h.DATACENTER_ID
, case when h.SOAP_PORT > 0 then h.SOAP_PORT else h.AUTHD_PORT end as PORT
, h.USER_NAME as USERNAME
, h.PASSWORD
, h.PASSWORD_LAST_UPD_DT
, h.ENABLED
, h.VMOTION_ENABLED
, h.HOST_VENDOR
, h.HOST_MODEL
, h.CPU_MODEL
, h.CPU_COUNT
, h.CPU_CORE_COUNT
, h.CPU_HZ
, h.CPU_THREAD_COUNT
, h.CPU_POWER_MGMT_POLICY
, h.CPU_POWER_MGMT_SUPPORT
, h.MEM_SIZE
, h.BOOT_TIME
, h.DNS_NAME
, h.PRODUCT_NAME
, h.PRODUCT_FULLNAME
, h.PRODUCT_VERSION
, h.PRODUCT_BUILD
, h.PRODUCT_VENDOR
, h.PRODUCT_OS_TYPE
, h.PRODUCT_API_TYPE
, h.PRODUCT_API_VERSION
, h.IP_ADDRESS
, h.UUID_BIOS
, h.LICENSED_EDITION
, h.NIC_COUNT
, h.HBA_COUNT
, h.DAS_NODE_NAME
, CASE H.POWER_STATE
      WHEN 0 THEN 'On'
      WHEN 1 THEN 'Off'
      WHEN 2 THEN 'Suspended'
      WHEN 3 THEN 'Disconnected'
  END AS POWER_STATE
, h.LOCAL_IP_ADDRESS
, h.HOST_KEY
, h.MANAGEMENT_IP
, CASE h.MAINTENANCE_MODE when 0 then 'NO' when 1 then 'YES' end as MMode
from VPX_ENTITY e, VPX_HOST h
where e.ID = h.ID;

/*==============================================================*/
/* View: VPXV_VMS                                               */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_VMS;

create or replace view VPXV_VMS as
select e.ID as VMID
, e.NAME
, e.PARENT_ID as VMGROUPID
, v.HOST_ID as HOSTID
, v.FILE_NAME as CONFIGFILENAME
, v.UUID_BIOS as VMUNIQUEID
, v.RESOURCE_GROUP_ID
, v.MEM_SIZE_MB
, v.NUM_VCPU
, v.BOOT_TIME
, v.SUSPEND_TIME
, case v.POWER_STATE when 0 then 'Off' when 1 then 'On' when 2 then 'Suspended' end as POWER_STATE
, v.GUEST_OS
, v.GUEST_FAMILY
, v.GUEST_STATE
, v.MEMORY_RESERVATION
, v.MEMORY_OVERHEAD
, v.CPU_RESERVATION
, v.DNS_NAME
, v.IP_ADDRESS
, case v.Tools_Status when 0 then 'Not Installed' when 1 then 'Not Running' when 2 then 'Old' when 3 then 'OK' end as VMMWARE_TOOL
, vmc.TOOLS_TOOLS_VERSION as TOOLS_VERSION
, v.NUM_NIC
, v.NUM_DISK
, v.IS_TEMPLATE
, vmc.ANNOTATION
, v.SUSPEND_INTERVAL
, v.AGGR_COMMITED_STORAGE_SPACE
, v.AGGR_UNCOMMITED_STORAGE_SPACE
, v.AGGR_UNSHARED_STORAGE_SPACE
, v.STORAGE_SPACE_UPDATED_TIME
from VPX_ENTITY e
JOIN VPX_VM v
    ON e.ID = v.ID
JOIN VPX_NON_ORM_VM_CONFIG_INFO vmc
    ON e.ID = vmc.ID;

/*==============================================================*/
/* View: VPXV_FARMGROUPS                                        */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_FARMGROUPS;

create or replace view VPXV_FARMGROUPS as
select e.ID as FARMGROUPID, e.NAME, e.PARENT_ID as PARENTID
from VPX_ENTITY e
where e.TYPE_ID = 7;

/*==============================================================*/
/* View: VPXV_VMGROUPS                                          */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_VMGROUPS;

create or replace view VPXV_VMGROUPS as
select e.ID as VMGROUPID, e.NAME, e.PARENT_ID as PARENTID
from VPX_ENTITY e
where e.TYPE_ID = 5;

/*==============================================================*/
/* View: VPXV_FIELDS                                            */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_FIELDS;

create or replace view VPXV_FIELDS as
select ID as FIELDID, NAME
from VPX_FIELD_DEF;

/*==============================================================*/
/* View: VPXV_HOST_FIELDS                                       */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_HOST_FIELDS;

create or replace view VPXV_HOST_FIELDS as
select FIELD_ID as FIELDID, e.ID as HOSTID, VALUE as FIELDVALUE
from VPX_FIELD_VAL v, VPX_ENTITY e
where v.MO_ID like 'host-%'
and CAST(coalesce(SUBSTRING(v.MO_ID from 6 for length(v.MO_ID)),null, '1') AS BIGINT) = e.ID
and e.TYPE_ID = 1;

/*==============================================================*/
/* View: VPXV_VM_FIELDS                                         */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_VM_FIELDS;

create or replace view VPXV_VM_FIELDS as
select FIELD_ID as FIELDID, e.ID as VMID, VALUE as FIELDVALUE
from VPX_FIELD_VAL v, VPX_ENTITY e
where v.MO_ID like 'vm-%'
and CAST(coalesce(SUBSTRING(v.MO_ID from 4 for length(v.MO_ID)),NULL, '1') AS BIGINT) = e.ID
and e.TYPE_ID = 0;

/*==============================================================*/
/* View: VPXV_ALARMS                                            */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_ALARMS;

create or replace view VPXV_ALARMS as
select ALARM_ID as ALARMID, NAME, ENABLED
from VPX_ALARM;

/*==============================================================*/
/* View: VPXV_TEMPLATES                                         */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_TEMPLATES;

create or replace view VPXV_TEMPLATES as
select e.ID as TEMPLATEID, e.NAME, v.ANNOTATION as DESCRIPTION
from VPX_ENTITY e
join VPX_NON_ORM_VM_CONFIG_INFO v
 on e.ID = v.ID
where v.TEMPLATE_FLG = 1;

/*==============================================================*/
/* View: VPXV_TASKS                                             */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_TASKS;

create or replace view VPXV_TASKS as
select t.SCHEDULEDTASK_ID as TASKID, NAME as NAME, ENABLED as ENABLED,
case SCHEDULER_TYPE when N'vim.scheduler.OnceTaskScheduler' then N'once'
                    when N'vim.scheduler.AfterStartupTaskScheduler' then N'afterStartup'
                    when N'vim.scheduler.HourlyTaskScheduler' then N'hourly'
                    when N'vim.scheduler.DailyTaskScheduler' then N'daily'
                    when N'vim.scheduler.WeeklyTaskScheduler' then N'weekly'
                    when N'vim.scheduler.MonthlyTaskScheduler' then N'monthly'
end as FREQUENCY,
case COMPLETE_STATE when N'success' then N'done'
                    when N'error' then N'error'
end as STATUS,
NOTIFICATION as SENDTO
from VPX_SCHEDULEDTASK t, VPX_SCHED_SCHEDULER s
where s.SCHEDULEDTASK_ID = t.SCHEDULEDTASK_ID;

/*==============================================================*/
/*          EVENT VIEWS                                         */
/*==============================================================*/

/* drop views section                                           */
DROP VIEW IF EXISTS VPXV_EVENTS;
DROP VIEW IF EXISTS VPXV_EVENT_ALL;
DROP VIEW IF EXISTS VPXV_EVENT_ARG CASCADE;
/* END of drop views section                                    */

/*==============================================================*/
/* View: VPXV_EVENT_ALL                                         */
/*==============================================================*/

CREATE OR REPLACE VIEW VPXV_EVENT_ALL
 AS
  SELECT *, 1 AS PARTITION_INDEX FROM VPX_EVENT_1
  UNION ALL
  SELECT *, 2 FROM VPX_EVENT_2
  UNION ALL
  SELECT *, 3 FROM VPX_EVENT_3
  UNION ALL
  SELECT *, 4 FROM VPX_EVENT_4
  UNION ALL
  SELECT *, 5 FROM VPX_EVENT_5
  UNION ALL
  SELECT *, 6 FROM VPX_EVENT_6
  UNION ALL
  SELECT *, 7 FROM VPX_EVENT_7
  UNION ALL
  SELECT *, 8 FROM VPX_EVENT_8
  UNION ALL
  SELECT *, 9 FROM VPX_EVENT_9
  UNION ALL
  SELECT *, 10 FROM VPX_EVENT_10
  UNION ALL
  SELECT *, 11 FROM VPX_EVENT_11
  UNION ALL
  SELECT *, 12 FROM VPX_EVENT_12
  UNION ALL
  SELECT *, 13 FROM VPX_EVENT_13
  UNION ALL
  SELECT *, 14 FROM VPX_EVENT_14
  UNION ALL
  SELECT *, 15 FROM VPX_EVENT_15
  UNION ALL
  SELECT *, 16 FROM VPX_EVENT_16
  UNION ALL
  SELECT *, 17 FROM VPX_EVENT_17
  UNION ALL
  SELECT *, 18 FROM VPX_EVENT_18
  UNION ALL
  SELECT *, 19 FROM VPX_EVENT_19
  UNION ALL
  SELECT *, 20 FROM VPX_EVENT_20
  UNION ALL
  SELECT *, 21 FROM VPX_EVENT_21
  UNION ALL
  SELECT *, 22 FROM VPX_EVENT_22
  UNION ALL
  SELECT *, 23 FROM VPX_EVENT_23
  UNION ALL
  SELECT *, 24 FROM VPX_EVENT_24
  UNION ALL
  SELECT *, 25 FROM VPX_EVENT_25
  UNION ALL
  SELECT *, 26 FROM VPX_EVENT_26
  UNION ALL
  SELECT *, 27 FROM VPX_EVENT_27
  UNION ALL
  SELECT *, 28 FROM VPX_EVENT_28
  UNION ALL
  SELECT *, 29 FROM VPX_EVENT_29
  UNION ALL
  SELECT *, 30 FROM VPX_EVENT_30
  UNION ALL
  SELECT *, 31 FROM VPX_EVENT_31
  UNION ALL
  SELECT *, 32 FROM VPX_EVENT_32
  UNION ALL
  SELECT *, 33 FROM VPX_EVENT_33
  UNION ALL
  SELECT *, 34 FROM VPX_EVENT_34
  UNION ALL
  SELECT *, 35 FROM VPX_EVENT_35
  UNION ALL
  SELECT *, 36 FROM VPX_EVENT_36
  UNION ALL
  SELECT *, 37 FROM VPX_EVENT_37
  UNION ALL
  SELECT *, 38 FROM VPX_EVENT_38
  UNION ALL
  SELECT *, 39 FROM VPX_EVENT_39
  UNION ALL
  SELECT *, 40 FROM VPX_EVENT_40
  UNION ALL
  SELECT *, 41 FROM VPX_EVENT_41
  UNION ALL
  SELECT *, 42 FROM VPX_EVENT_42
  UNION ALL
  SELECT *, 43 FROM VPX_EVENT_43
  UNION ALL
  SELECT *, 44 FROM VPX_EVENT_44
  UNION ALL
  SELECT *, 45 FROM VPX_EVENT_45
  UNION ALL
  SELECT *, 46 FROM VPX_EVENT_46
  UNION ALL
  SELECT *, 47 FROM VPX_EVENT_47
  UNION ALL
  SELECT *, 48 FROM VPX_EVENT_48
  UNION ALL
  SELECT *, 49 FROM VPX_EVENT_49
  UNION ALL
  SELECT *, 50 FROM VPX_EVENT_50
  UNION ALL
  SELECT *, 51 FROM VPX_EVENT_51
  UNION ALL
  SELECT *, 52 FROM VPX_EVENT_52
  UNION ALL
  SELECT *, 53 FROM VPX_EVENT_53
  UNION ALL
  SELECT *, 54 FROM VPX_EVENT_54
  UNION ALL
  SELECT *, 55 FROM VPX_EVENT_55
  UNION ALL
  SELECT *, 56 FROM VPX_EVENT_56
  UNION ALL
  SELECT *, 57 FROM VPX_EVENT_57
  UNION ALL
  SELECT *, 58 FROM VPX_EVENT_58
  UNION ALL
  SELECT *, 59 FROM VPX_EVENT_59
  UNION ALL
  SELECT *, 60 FROM VPX_EVENT_60
  UNION ALL
  SELECT *, 61 FROM VPX_EVENT_61
  UNION ALL
  SELECT *, 62 FROM VPX_EVENT_62
  UNION ALL
  SELECT *, 63 FROM VPX_EVENT_63
  UNION ALL
  SELECT *, 64 FROM VPX_EVENT_64
  UNION ALL
  SELECT *, 65 FROM VPX_EVENT_65
  UNION ALL
  SELECT *, 66 FROM VPX_EVENT_66
  UNION ALL
  SELECT *, 67 FROM VPX_EVENT_67
  UNION ALL
  SELECT *, 68 FROM VPX_EVENT_68
  UNION ALL
  SELECT *, 69 FROM VPX_EVENT_69
  UNION ALL
  SELECT *, 70 FROM VPX_EVENT_70
  UNION ALL
  SELECT *, 71 FROM VPX_EVENT_71
  UNION ALL
  SELECT *, 72 FROM VPX_EVENT_72
  UNION ALL
  SELECT *, 73 FROM VPX_EVENT_73
  UNION ALL
  SELECT *, 74 FROM VPX_EVENT_74
  UNION ALL
  SELECT *, 75 FROM VPX_EVENT_75
  UNION ALL
  SELECT *, 76 FROM VPX_EVENT_76
  UNION ALL
  SELECT *, 77 FROM VPX_EVENT_77
  UNION ALL
  SELECT *, 78 FROM VPX_EVENT_78
  UNION ALL
  SELECT *, 79 FROM VPX_EVENT_79
  UNION ALL
  SELECT *, 80 FROM VPX_EVENT_80
  UNION ALL
  SELECT *, 81 FROM VPX_EVENT_81
  UNION ALL
  SELECT *, 82 FROM VPX_EVENT_82
  UNION ALL
  SELECT *, 83 FROM VPX_EVENT_83
  UNION ALL
  SELECT *, 84 FROM VPX_EVENT_84
  UNION ALL
  SELECT *, 85 FROM VPX_EVENT_85
  UNION ALL
  SELECT *, 86 FROM VPX_EVENT_86
  UNION ALL
  SELECT *, 87 FROM VPX_EVENT_87
  UNION ALL
  SELECT *, 88 FROM VPX_EVENT_88
  UNION ALL
  SELECT *, 89 FROM VPX_EVENT_89
  UNION ALL
  SELECT *, 90 FROM VPX_EVENT_90
  UNION ALL
  SELECT *, 91 FROM VPX_EVENT_91
  UNION ALL
  SELECT *, 92 FROM VPX_EVENT_92
;
/*==============================================================*/
/* View: VPXV_EVENT_ARG                                         */
/*==============================================================*/

CREATE OR REPLACE VIEW VPXV_EVENT_ARG
 AS
  SELECT *, 1 AS PARTITION_INDEX FROM VPX_EVENT_ARG_1
  UNION ALL
  SELECT *, 2 FROM VPX_EVENT_ARG_2
  UNION ALL
  SELECT *, 3 FROM VPX_EVENT_ARG_3
  UNION ALL
  SELECT *, 4 FROM VPX_EVENT_ARG_4
  UNION ALL
  SELECT *, 5 FROM VPX_EVENT_ARG_5
  UNION ALL
  SELECT *, 6 FROM VPX_EVENT_ARG_6
  UNION ALL
  SELECT *, 7 FROM VPX_EVENT_ARG_7
  UNION ALL
  SELECT *, 8 FROM VPX_EVENT_ARG_8
  UNION ALL
  SELECT *, 9 FROM VPX_EVENT_ARG_9
  UNION ALL
  SELECT *, 10 FROM VPX_EVENT_ARG_10
  UNION ALL
  SELECT *, 11 FROM VPX_EVENT_ARG_11
  UNION ALL
  SELECT *, 12 FROM VPX_EVENT_ARG_12
  UNION ALL
  SELECT *, 13 FROM VPX_EVENT_ARG_13
  UNION ALL
  SELECT *, 14 FROM VPX_EVENT_ARG_14
  UNION ALL
  SELECT *, 15 FROM VPX_EVENT_ARG_15
  UNION ALL
  SELECT *, 16 FROM VPX_EVENT_ARG_16
  UNION ALL
  SELECT *, 17 FROM VPX_EVENT_ARG_17
  UNION ALL
  SELECT *, 18 FROM VPX_EVENT_ARG_18
  UNION ALL
  SELECT *, 19 FROM VPX_EVENT_ARG_19
  UNION ALL
  SELECT *, 20 FROM VPX_EVENT_ARG_20
  UNION ALL
  SELECT *, 21 FROM VPX_EVENT_ARG_21
  UNION ALL
  SELECT *, 22 FROM VPX_EVENT_ARG_22
  UNION ALL
  SELECT *, 23 FROM VPX_EVENT_ARG_23
  UNION ALL
  SELECT *, 24 FROM VPX_EVENT_ARG_24
  UNION ALL
  SELECT *, 25 FROM VPX_EVENT_ARG_25
  UNION ALL
  SELECT *, 26 FROM VPX_EVENT_ARG_26
  UNION ALL
  SELECT *, 27 FROM VPX_EVENT_ARG_27
  UNION ALL
  SELECT *, 28 FROM VPX_EVENT_ARG_28
  UNION ALL
  SELECT *, 29 FROM VPX_EVENT_ARG_29
  UNION ALL
  SELECT *, 30 FROM VPX_EVENT_ARG_30
  UNION ALL
  SELECT *, 31 FROM VPX_EVENT_ARG_31
  UNION ALL
  SELECT *, 32 FROM VPX_EVENT_ARG_32
  UNION ALL
  SELECT *, 33 FROM VPX_EVENT_ARG_33
  UNION ALL
  SELECT *, 34 FROM VPX_EVENT_ARG_34
  UNION ALL
  SELECT *, 35 FROM VPX_EVENT_ARG_35
  UNION ALL
  SELECT *, 36 FROM VPX_EVENT_ARG_36
  UNION ALL
  SELECT *, 37 FROM VPX_EVENT_ARG_37
  UNION ALL
  SELECT *, 38 FROM VPX_EVENT_ARG_38
  UNION ALL
  SELECT *, 39 FROM VPX_EVENT_ARG_39
  UNION ALL
  SELECT *, 40 FROM VPX_EVENT_ARG_40
  UNION ALL
  SELECT *, 41 FROM VPX_EVENT_ARG_41
  UNION ALL
  SELECT *, 42 FROM VPX_EVENT_ARG_42
  UNION ALL
  SELECT *, 43 FROM VPX_EVENT_ARG_43
  UNION ALL
  SELECT *, 44 FROM VPX_EVENT_ARG_44
  UNION ALL
  SELECT *, 45 FROM VPX_EVENT_ARG_45
  UNION ALL
  SELECT *, 46 FROM VPX_EVENT_ARG_46
  UNION ALL
  SELECT *, 47 FROM VPX_EVENT_ARG_47
  UNION ALL
  SELECT *, 48 FROM VPX_EVENT_ARG_48
  UNION ALL
  SELECT *, 49 FROM VPX_EVENT_ARG_49
  UNION ALL
  SELECT *, 50 FROM VPX_EVENT_ARG_50
  UNION ALL
  SELECT *, 51 FROM VPX_EVENT_ARG_51
  UNION ALL
  SELECT *, 52 FROM VPX_EVENT_ARG_52
  UNION ALL
  SELECT *, 53 FROM VPX_EVENT_ARG_53
  UNION ALL
  SELECT *, 54 FROM VPX_EVENT_ARG_54
  UNION ALL
  SELECT *, 55 FROM VPX_EVENT_ARG_55
  UNION ALL
  SELECT *, 56 FROM VPX_EVENT_ARG_56
  UNION ALL
  SELECT *, 57 FROM VPX_EVENT_ARG_57
  UNION ALL
  SELECT *, 58 FROM VPX_EVENT_ARG_58
  UNION ALL
  SELECT *, 59 FROM VPX_EVENT_ARG_59
  UNION ALL
  SELECT *, 60 FROM VPX_EVENT_ARG_60
  UNION ALL
  SELECT *, 61 FROM VPX_EVENT_ARG_61
  UNION ALL
  SELECT *, 62 FROM VPX_EVENT_ARG_62
  UNION ALL
  SELECT *, 63 FROM VPX_EVENT_ARG_63
  UNION ALL
  SELECT *, 64 FROM VPX_EVENT_ARG_64
  UNION ALL
  SELECT *, 65 FROM VPX_EVENT_ARG_65
  UNION ALL
  SELECT *, 66 FROM VPX_EVENT_ARG_66
  UNION ALL
  SELECT *, 67 FROM VPX_EVENT_ARG_67
  UNION ALL
  SELECT *, 68 FROM VPX_EVENT_ARG_68
  UNION ALL
  SELECT *, 69 FROM VPX_EVENT_ARG_69
  UNION ALL
  SELECT *, 70 FROM VPX_EVENT_ARG_70
  UNION ALL
  SELECT *, 71 FROM VPX_EVENT_ARG_71
  UNION ALL
  SELECT *, 72 FROM VPX_EVENT_ARG_72
  UNION ALL
  SELECT *, 73 FROM VPX_EVENT_ARG_73
  UNION ALL
  SELECT *, 74 FROM VPX_EVENT_ARG_74
  UNION ALL
  SELECT *, 75 FROM VPX_EVENT_ARG_75
  UNION ALL
  SELECT *, 76 FROM VPX_EVENT_ARG_76
  UNION ALL
  SELECT *, 77 FROM VPX_EVENT_ARG_77
  UNION ALL
  SELECT *, 78 FROM VPX_EVENT_ARG_78
  UNION ALL
  SELECT *, 79 FROM VPX_EVENT_ARG_79
  UNION ALL
  SELECT *, 80 FROM VPX_EVENT_ARG_80
  UNION ALL
  SELECT *, 81 FROM VPX_EVENT_ARG_81
  UNION ALL
  SELECT *, 82 FROM VPX_EVENT_ARG_82
  UNION ALL
  SELECT *, 83 FROM VPX_EVENT_ARG_83
  UNION ALL
  SELECT *, 84 FROM VPX_EVENT_ARG_84
  UNION ALL
  SELECT *, 85 FROM VPX_EVENT_ARG_85
  UNION ALL
  SELECT *, 86 FROM VPX_EVENT_ARG_86
  UNION ALL
  SELECT *, 87 FROM VPX_EVENT_ARG_87
  UNION ALL
  SELECT *, 88 FROM VPX_EVENT_ARG_88
  UNION ALL
  SELECT *, 89 FROM VPX_EVENT_ARG_89
  UNION ALL
  SELECT *, 90 FROM VPX_EVENT_ARG_90
  UNION ALL
  SELECT *, 91 FROM VPX_EVENT_ARG_91
  UNION ALL
  SELECT *, 92 FROM VPX_EVENT_ARG_92
;

/*==============================================================*/
/* View: VPXV_EVENTS                                            */
/*==============================================================*/
CREATE OR REPLACE VIEW VPXV_EVENTS AS
select
EVENT_ID
, CHAIN_ID
, EVENT_TYPE
, EXTENDED_CLASS
, CREATE_TIME
, USERNAME
, CATEGORY
, VM_ID
, VM_NAME
, HOST_ID
, HOST_NAME
, COMPUTERESOURCE_ID
, COMPUTERESOURCE_TYPE
, COMPUTERESOURCE_NAME
, DATACENTER_ID
, DATACENTER_NAME
, DATASTORE_ID
, DATASTORE_NAME
, NETWORK_ID
, NETWORK_NAME
, NETWORK_TYPE
, DVS_ID
, DVS_NAME
from VPXV_EVENT_ALL;

/*==============================================================*/
/*      END EVENT VIEWS                                         */
/*==============================================================*/

/*==============================================================*/
/* View: VPXV_ENTITY_MOID                                       */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_ENTITY_MOID;

create or replace view VPXV_ENTITY_MOID as
select ID as ENTITYID,
      case
                  when type_id=0 then 'vm-'||ID
                  when type_id=1 then 'host-'||ID
                  when type_id=2 then 'domain-s'||ID
                  when type_id=3 then 'domain-c'||ID
                  when type_id=4 then 'resgroup-'||ID
		  when type_id=5 then 'group-v'|| ID
		  when type_id=6 then 'group-h'|| ID
		  when type_id=7 then 'group-d'|| ID
		  when type_id=8 then 'datacenter-'|| ID
		  when type_id=9 then 'alarm-'|| ID
		  when type_id=10 then 'schedule-'|| ID
		  when type_id=11 then 'task-'|| ID
		  when type_id=12 then 'extension-'|| ID
		  when type_id=13 then 'resgroup-v'|| ID
		  when type_id=14 then 'dvs-'|| ID
		  when type_id=15 then 'dvportgroup-'|| ID
		  when type_id=16 then 'group-s'|| ID
		  when type_id=17 then 'group-n'|| ID
		  when type_id=18 then 'datastore-'|| ID
		  when type_id=19 then 'network-'|| ID
		  when type_id=21 then 'network-o'|| ID
      else 'null'
      end AS MOID
from VPX_ENTITY;

/*==============================================================*/
/* View: VPXV_DS_LUN_INFO                                       */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_DS_LUN_INFO;

create or replace view VPXV_DS_LUN_INFO AS
select sl.uuid, sl.canonical_name, sl.HOST_ID, di.url, di.forceMount_flg, di.ID as DATASTORE_ID, sld.QUALITY
from VPX_HOST_SCSI_LUN sl
, VPX_HOST_SCSI_DISK_PARTITION sdp
, VPX_DS_INFO di
, VPX_HOST_SCSILUN_DESCRIPTOR sld
where sl.canonical_name = sdp.disk_name
and sl.host_id = sdp.host_id
and sdp.vmfs_uuid = di.vmfs_uuid
and sl.ID=sld.ID
and sl.uuid=sld.DESCRIPTOR_ID_VAL
;

/*==============================================================*/
/* View: VPXV_VM_RDM_LUN_INFO                                   */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_VM_RDM_LUN_INFO;

create or replace view VPXV_VM_RDM_LUN_INFO AS
select VM_ID, DEVICE_BACKING_LUN_UUID from VPX_VDEVICE_FILE_BACKING;

/*==============================================================*/
/* View: VPXV_SN_RDM_LUN_INFO                                   */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_SN_RDM_LUN_INFO;

create or replace view VPXV_SN_RDM_LUN_INFO AS
select SNAPSHOT_ID, DEVICE_BACKING_LUN_UUID from VPX_SN_VDEVICE_FILE_BACKING;

/*==============================================================*/
/* View: VPXV_DATASTORE                                         */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_DATASTORE;

create or replace view VPXV_DATASTORE AS
SELECT ID
, NAME
, STORAGE_URL
, CAPACITY
, FREE_SPACE
, TYPE
, DATACENTER_ID
, IORM_ENABLED
FROM VPX_DATASTORE;

/*==============================================================*/
/* View: VPXV_HOST_CPU                                          */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_HOST_CPU;

create or replace view VPXV_HOST_CPU as
select e.ID as HOSTID
, e.NAME as NAME
, h.CPU_INDEX as CPU_PACKAGE_INDEX
, h.HZ as CPU_SPEED
, h.BUS_HZ as BUS_SPEED
, h.DESCRIPTION as CPU_DESCRIPTION
, h.VENDOR as CPU_VENDOR_NAME
from VPX_ENTITY e, VPX_HOST_CPU h
where e.ID = h.HOST_ID;

/*==============================================================*/
/* View: VPXV_OBJECT_TYPE                                       */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_OBJECT_TYPE;

create or replace view VPXV_OBJECT_TYPE as
select ID as OBJECTID
, NAME as OBJECTNAME
from VPX_OBJECT_TYPE;

/*==============================================================*/
/* View: VPXV_CUSTOMIZED_FIELD                                  */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_CUSTOMIZED_FIELD;

create or replace view VPXV_CUSTOMIZED_FIELD AS
select
d.NAME as CUST_FIELD_NAME
, v.VALUE  as CUST_FIELD_VALUE
, e.ID as ENTITY_ID
, e.NAME as ENTITY_NAME
from VPX_FIELD_VAL v, VPX_ENTITY e, VPX_FIELD_DEF d
where v.FIELD_ID=d.ID
and   v.ENTITY_ID=e.ID;

/*==============================================================*/
/* View: VPXV_COMPUTE_RESOURCE_DAS_VM                           */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_COMPUTE_RESOURCE_DAS_VM;

create or replace view VPXV_COMPUTE_RESOURCE_DAS_VM AS
select e.ID as RESOURCEPOOLID
, e.NAME as NAME
, e.PARENT_ID
, cdasv.VM_ID
, cdasv.PRIORITY
, cdasv.NUM_FAILURE
, cdasv.RETRY_PERIOD_SEC
, cdasv.POWER_OFF_ON_ISOLATION
, cdasv.VMHEALTH_VM_MONITORING
, cdasv.VMHEALTH_FAILURE_INTERVAL
, cdasv.VMHEALTH_MIN_UPTIME
, cdasv.VMHEALTH_MAX_FAILURES
, cdasv.VMHEALTH_MAX_FAILURE_WINDOW
from VPX_ENTITY e, VPX_COMPUTE_RESOURCE_DAS_VM cdasv
where e.ID=cdasv.COMP_RES_ID;

/*==============================================================*/
/* View: VPXV_COMPUTE_RESOURCE_DRS_VM                           */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_COMPUTE_RESOURCE_DRS_VM;

create or replace view VPXV_COMPUTE_RESOURCE_DRS_VM AS
select e.ID as RESOURCEPOOLID
, e.NAME as NAME
, e.PARENT_ID
, cdrsv.ENABLED AS DRS_ENABLED
, cdrsv.BEHAVIOR as DRS_BEHAVIOR
from VPX_ENTITY e, VPX_COMPUTE_RESOURCE_DRS_VM cdrsv
where e.ID=cdrsv.COMP_RES_ID;

/*==============================================================*/
/* View: VPXV_ENTITY_VMOP_COUNT                                 */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_ENTITY_VMOP_COUNT;

create or replace view VPXV_ENTITY_VMOP_COUNT AS
select e.ID as RESOURCEPOOLID
, e.NAME as NAME
, e.PARENT_ID
, evc.POWERON_COUNT
, evc.POWEROFF_COUNT
, evc.SUSPEND_COUNT
, evc.RESET_COUNT
, evc.REBOOTGUEST_COUNT
, evc.STANDBYGUEST_COUNT
, evc.SHUTDOWNGUEST_COUNT
, evc.CREATE_COUNT
, evc.DESTROY_COUNT
, evc.REGISTER_COUNT
, evc.UNREGISTER_COUNT
, evc.RECONFIGURE_COUNT
, evc.CLONE_COUNT
, evc.DEPLOY_COUNT
, evc.CHANGEHOST_COUNT
, evc.CHANGEDS_COUNT
, evc.CHANGEHOSTDS_COUNT
, evc.VMOTION_COUNT
, evc.SVMOTION_COUNT
from VPX_ENTITY e, VPX_ENTITY_VMOP_COUNT evc
where e.ID=evc.ID;

/*==============================================================*/
/* View: VPXV_SNAPSHOT                                          */
/*==============================================================*/
DROP VIEW IF EXISTS VPXV_SNAPSHOT;

create or replace view VPXV_SNAPSHOT AS
select
s.ID as SNAPSHOT_ID
, s.HOST_SNAPSHOT_ID
, s.VM_ID
, s.SNAPSHOT_NAME
, s.CREATE_TIME
, s.POWER_STATE
, s.IS_QUIESCED
, s.SNAPSHOT_UID
, s.PARENT_SNAPSHOT_ID
, s.IS_CURRENT_SNAPSHOT
from VPX_SNAPSHOT s
;
