<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
					  

	
	<changeSet id="0001" author="saliakhov">
        <comment>edit table</comment>
		<sql>
			DROP VIEW IF EXISTS actualization.view_app_reason; 

			ALTER TABLE actualization.app_reason ADD COLUMN IF NOT EXISTS count_all int8 DEFAULT 0 NOT NULL;

			CREATE OR REPLACE VIEW actualization.view_app_reason
			AS SELECT app_reason.app_reason_id,
				round(app_reason.count_place_of_stay::numeric * 100::numeric / (app_reason.count_all)::numeric) AS percent_place_of_stay,
				round(app_reason.count_work::numeric * 100::numeric / (app_reason.count_all)::numeric) AS percent_work,
				round(app_reason.count_place_of_study::numeric * 100::numeric / (app_reason.count_all)::numeric) AS percent_place_of_study,
				round(app_reason.count_family_status::numeric * 100::numeric / (app_reason.count_all)::numeric) AS percent_family_status,
				round(app_reason.count_education::numeric * 100::numeric / (app_reason.count_all)::numeric) AS percent_education,
				round(app_reason.count_renaming::numeric * 100::numeric / (app_reason.count_all)::numeric) AS percent_renaming
			   FROM actualization.app_reason;
		</sql>
	</changeSet>
	
	<changeSet id="0002" author="saliakhov">
        <comment>create table</comment>
		<sql>
			CREATE TABLE IF NOT EXISTS init_registration_info.incoming_creating_records (
			incoming_creating_records_id bigserial NOT NULL,
			recruitment_id varchar(36) NOT NULL constraint fk_conv_info_records_from_easu_recruitment_id references metrics.recruitment,
			update_date timestamp DEFAULT now() NOT NULL,
			info_date date NOT NULL,
			records_gir_vu int8 DEFAULT 0 NOT NULL,
			records_citizen_create int8 DEFAULT 0 NOT NULL,
			CONSTRAINT incoming_creating_records_pkey PRIMARY KEY (incoming_creating_records_id)
		);

		ALTER TABLE IF EXISTS init_registration_info.incoming_creating_records OWNER TO ervu_business_metrics;

		CREATE INDEX IF NOT EXISTS idx_incoming_creating_records_date ON init_registration_info.incoming_creating_records USING btree (info_date);
		CREATE INDEX IF NOT EXISTS idx_incoming_creating_records_recr ON init_registration_info.incoming_creating_records USING btree (recruitment_id);
		CREATE INDEX IF NOT EXISTS idx_incoming_creating_records_recr_date ON init_registration_info.incoming_creating_records USING btree (recruitment_id, info_date);

		COMMENT ON TABLE init_registration_info.incoming_creating_records IS 'Первоначальная постановка на учет. Информация о записях поступивших из ГИР ВУ и созданых гражданина';
		COMMENT ON COLUMN init_registration_info.incoming_creating_records.records_gir_vu IS 'записей поступило из ГИР ВУ';
		COMMENT ON COLUMN init_registration_info.incoming_creating_records.records_citizen_create IS 'записей гражданина создано';
		</sql>
	</changeSet>
</databaseChangeLog>