<?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>create table</comment>
		<sql>
			CREATE TABLE IF NOT EXISTS deregistration.decisions_signing (
			decisions_signing_id bigserial NOT NULL,
			recruitment_id varchar(36) NOT NULL,
			update_date timestamp DEFAULT now() NOT NULL,
			info_date date NOT NULL,
			count_arrived_death_reason int8 DEFAULT 0 NOT NULL,
			count_arrived_age_limit int8 DEFAULT 0 NOT NULL,
			CONSTRAINT decisions_signing_pkey PRIMARY KEY (decisions_signing_id),
			CONSTRAINT fk_decisions_signing_recruitment_id FOREIGN KEY (recruitment_id) REFERENCES metrics.recruitment(id)
			);
			CREATE INDEX IF NOT EXISTS idx_decisions_signing_date ON deregistration.decisions_signing USING btree (info_date);
			CREATE INDEX IF NOT EXISTS idx_decisions_signing_recr ON deregistration.decisions_signing USING btree (recruitment_id);
			CREATE INDEX IF NOT EXISTS idx_decisions_signing_recr_date ON deregistration.decisions_signing USING btree (recruitment_id, info_date);

			COMMENT ON TABLE deregistration.decisions_signing IS 'Снятие с воинского учета. Решения на подписании';
			COMMENT ON COLUMN deregistration.decisions_signing.count_arrived_death_reason IS 'поступило по причине смерти';
			COMMENT ON COLUMN deregistration.decisions_signing.count_arrived_age_limit IS 'поступило по предельному возрасту';

			ALTER TABLE IF EXISTS deregistration.decisions_signing OWNER TO ervu_business_metrics;

			CREATE OR REPLACE VIEW deregistration.view_decisions_signing
			AS SELECT decisions_signing.decisions_signing_id,
				decisions_signing.count_arrived_death_reason + decisions_signing.count_arrived_age_limit AS count_arrived_all,
				round(decisions_signing.count_arrived_death_reason::numeric * 100::numeric / (decisions_signing.count_arrived_death_reason + decisions_signing.count_arrived_age_limit)::numeric) AS percent_arrived_death_reason,
				round(decisions_signing.count_arrived_age_limit::numeric * 100::numeric / (decisions_signing.count_arrived_death_reason + decisions_signing.count_arrived_age_limit)::numeric) AS percent_arrived_age_limit
			   FROM deregistration.decisions_signing;

			ALTER TABLE deregistration.view_decisions_signing OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
	
	<changeSet id="0002" author="saliakhov">
        <comment>create view</comment>
		<sql>
			CREATE OR REPLACE VIEW registration_change_address.view_records_about_registrated_citizen_v2
			AS SELECT applications_received_from_epgu.applications_received_from_epgu_id,
				applications_received_from_epgu.count_provided + applications_received_from_epgu.count_registration_refusal + applications_received_from_epgu.count_refusal_provide_service AS count_all,
				coalesce(round(applications_received_from_epgu.count_provided::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_provided + applications_received_from_epgu.count_registration_refusal + applications_received_from_epgu.count_refusal_provide_service)::numeric,0)),0) AS percent_registrated_by_app_vk,
				coalesce(round((applications_received_from_epgu.count_registration_refusal + applications_received_from_epgu.count_refusal_provide_service)::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_provided + applications_received_from_epgu.count_registration_refusal + applications_received_from_epgu.count_refusal_provide_service)::numeric,0)),0) AS percent_registrated_another_vk,
				applications_received_from_epgu.count_provided::numeric as count_registrated_by_app_vk,
				(applications_received_from_epgu.count_registration_refusal + applications_received_from_epgu.count_refusal_provide_service)::numeric as count_registrated_another_vk
				FROM registration_change_address.applications_received_from_epgu;

			ALTER TABLE registration_change_address.view_records_about_registrated_citizen_v2 OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
	
	<changeSet id="0003" author="saliakhov">
        <comment>edit view actualization</comment>
		<sql>
			CREATE OR REPLACE VIEW actualization.view_app_reason
				AS SELECT app_reason.app_reason_id,
				coalesce(round(app_reason.count_place_of_stay::numeric * 100::numeric / NULLIF((app_reason.count_all)::numeric,0)),0) AS percent_place_of_stay,
				coalesce(round(app_reason.count_work::numeric * 100::numeric / NULLIF((app_reason.count_all)::numeric,0)),0) AS percent_work,
				coalesce(round(app_reason.count_place_of_study::numeric * 100::numeric / NULLIF((app_reason.count_all)::numeric,0)),0) AS percent_place_of_study,
				coalesce(round(app_reason.count_family_status::numeric * 100::numeric / NULLIF((app_reason.count_all)::numeric,0)),0) AS percent_family_status,
				coalesce(round(app_reason.count_education::numeric * 100::numeric / NULLIF((app_reason.count_all)::numeric,0)),0) AS percent_education,
				coalesce(round(app_reason.count_renaming::numeric * 100::numeric / NULLIF((app_reason.count_all)::numeric,0)),0) AS percent_renaming
			   FROM actualization.app_reason;

			ALTER TABLE actualization.view_app_reason OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW actualization.view_applications_received_from_epgu
			AS SELECT applications_received_from_epgu.applications_received_from_epgu_id,
				coalesce(round(applications_received_from_epgu.count_executor_appointed::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_appointed,
				coalesce(round(applications_received_from_epgu.count_executor_not_appointed_expired::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_not_appointed_expired,
				coalesce(round(applications_received_from_epgu.count_executor_not_appointed_on_time::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_not_appointed_on_time,
				coalesce(round(applications_received_from_epgu.count_registered::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_registered,
				coalesce(round(applications_received_from_epgu.count_registration_refusal::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_registration_refusal,
				coalesce(round(applications_received_from_epgu.count_refusal_provide_service::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_refusal_provide_service,
				coalesce(round(applications_received_from_epgu.count_provided::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_provided,
				coalesce(round(applications_received_from_epgu.count_received_department::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_received_department
			   FROM actualization.applications_received_from_epgu;

			ALTER TABLE actualization.view_applications_received_from_epgu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW actualization.view_change_data_from_gir_vu
			AS SELECT change_data_from_gir_vu.change_data_from_gir_vu_id,
				change_data_from_gir_vu.count_reg_address + change_data_from_gir_vu.count_place_of_stay + change_data_from_gir_vu.count_work + change_data_from_gir_vu.count_place_of_study + change_data_from_gir_vu.count_family_status + change_data_from_gir_vu.count_education + change_data_from_gir_vu.count_renaming + change_data_from_gir_vu.count_med_info + change_data_from_gir_vu.count_other AS count_all,
				coalesce(round(change_data_from_gir_vu.count_reg_address::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_reg_address + change_data_from_gir_vu.count_place_of_stay + change_data_from_gir_vu.count_work + change_data_from_gir_vu.count_place_of_study + change_data_from_gir_vu.count_family_status + change_data_from_gir_vu.count_education + change_data_from_gir_vu.count_renaming + change_data_from_gir_vu.count_med_info + change_data_from_gir_vu.count_other)::numeric,0)),0) AS percent_reg_address,
				coalesce(round(change_data_from_gir_vu.count_place_of_stay::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_reg_address + change_data_from_gir_vu.count_place_of_stay + change_data_from_gir_vu.count_work + change_data_from_gir_vu.count_place_of_study + change_data_from_gir_vu.count_family_status + change_data_from_gir_vu.count_education + change_data_from_gir_vu.count_renaming + change_data_from_gir_vu.count_med_info + change_data_from_gir_vu.count_other)::numeric,0)),0) AS percent_place_of_stay,
				coalesce(round(change_data_from_gir_vu.count_work::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_reg_address + change_data_from_gir_vu.count_place_of_stay + change_data_from_gir_vu.count_work + change_data_from_gir_vu.count_place_of_study + change_data_from_gir_vu.count_family_status + change_data_from_gir_vu.count_education + change_data_from_gir_vu.count_renaming + change_data_from_gir_vu.count_med_info + change_data_from_gir_vu.count_other)::numeric,0)),0) AS percent_work,
				coalesce(round(change_data_from_gir_vu.count_place_of_study::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_reg_address + change_data_from_gir_vu.count_place_of_stay + change_data_from_gir_vu.count_work + change_data_from_gir_vu.count_place_of_study + change_data_from_gir_vu.count_family_status + change_data_from_gir_vu.count_education + change_data_from_gir_vu.count_renaming + change_data_from_gir_vu.count_med_info + change_data_from_gir_vu.count_other)::numeric,0)),0) AS percent_place_of_study,
				coalesce(round(change_data_from_gir_vu.count_family_status::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_reg_address + change_data_from_gir_vu.count_place_of_stay + change_data_from_gir_vu.count_work + change_data_from_gir_vu.count_place_of_study + change_data_from_gir_vu.count_family_status + change_data_from_gir_vu.count_education + change_data_from_gir_vu.count_renaming + change_data_from_gir_vu.count_med_info + change_data_from_gir_vu.count_other)::numeric,0)),0) AS percent_family_status,
				coalesce(round(change_data_from_gir_vu.count_education::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_reg_address + change_data_from_gir_vu.count_place_of_stay + change_data_from_gir_vu.count_work + change_data_from_gir_vu.count_place_of_study + change_data_from_gir_vu.count_family_status + change_data_from_gir_vu.count_education + change_data_from_gir_vu.count_renaming + change_data_from_gir_vu.count_med_info + change_data_from_gir_vu.count_other)::numeric,0)),0) AS percent_education,
				coalesce(round(change_data_from_gir_vu.count_renaming::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_reg_address + change_data_from_gir_vu.count_place_of_stay + change_data_from_gir_vu.count_work + change_data_from_gir_vu.count_place_of_study + change_data_from_gir_vu.count_family_status + change_data_from_gir_vu.count_education + change_data_from_gir_vu.count_renaming + change_data_from_gir_vu.count_med_info + change_data_from_gir_vu.count_other)::numeric,0)),0) AS percent_renaming,
				coalesce(round(change_data_from_gir_vu.count_med_info::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_reg_address + change_data_from_gir_vu.count_place_of_stay + change_data_from_gir_vu.count_work + change_data_from_gir_vu.count_place_of_study + change_data_from_gir_vu.count_family_status + change_data_from_gir_vu.count_education + change_data_from_gir_vu.count_renaming + change_data_from_gir_vu.count_med_info + change_data_from_gir_vu.count_other)::numeric,0)),0) AS percent_med_info,
				coalesce(round(change_data_from_gir_vu.count_other::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_reg_address + change_data_from_gir_vu.count_place_of_stay + change_data_from_gir_vu.count_work + change_data_from_gir_vu.count_place_of_study + change_data_from_gir_vu.count_family_status + change_data_from_gir_vu.count_education + change_data_from_gir_vu.count_renaming + change_data_from_gir_vu.count_med_info + change_data_from_gir_vu.count_other)::numeric,0)),0) AS percent_other
			   FROM actualization.change_data_from_gir_vu;

			ALTER TABLE actualization.view_change_data_from_gir_vu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW actualization.view_change_data_personal_visit
			AS SELECT t.change_data_personal_visit_id,
				t.count_all,
				coalesce(round(t.count_for_sign::numeric * 100::numeric / NULLIF((t.count_all)::numeric,0)),0) AS percent_for_sign,
				coalesce(round(t.count_signed::numeric * 100::numeric / NULLIF((t.count_all)::numeric,0)),0) AS percent_signed,
				coalesce(round(t.count_refused::numeric * 100::numeric / NULLIF((t.count_all)::numeric,0)),0) AS percent_refused
			   FROM ( SELECT change_data_personal_visit.count_for_sign + change_data_personal_visit.count_signed + change_data_personal_visit.count_refused AS count_all,
						change_data_personal_visit.change_data_personal_visit_id,
						change_data_personal_visit.recruitment_id,
						change_data_personal_visit.update_date,
						change_data_personal_visit.info_date,
						change_data_personal_visit.count_for_sign,
						change_data_personal_visit.count_signed,
						change_data_personal_visit.count_refused
					   FROM actualization.change_data_personal_visit) t;

			ALTER TABLE actualization.view_change_data_personal_visit OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW actualization.view_incidents_epgu_info
			AS SELECT incidents_epgu_info.incidents_epgu_info_id,
				coalesce(round(incidents_epgu_info.count_resolved::numeric * 100::numeric / NULLIF((incidents_epgu_info.count_registered)::numeric,0)),0) AS percent_resolved,
				incidents_epgu_info.count_registered - incidents_epgu_info.count_resolved AS count_not_resolved
			   FROM actualization.incidents_epgu_info;

			ALTER TABLE actualization.view_incidents_epgu_info OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW actualization.view_incidents_info
			AS SELECT incidents_info.incidents_info_id,
				incidents_info.count_without_id_ern + incidents_info.count_discrepancy_epgu_info AS count_all,
				coalesce(round(incidents_info.count_without_id_ern::numeric * 100::numeric / NULLIF((incidents_info.count_without_id_ern + incidents_info.count_discrepancy_epgu_info)::numeric,0)),0) AS percent_without_id_ern,
				coalesce(round(incidents_info.count_discrepancy_epgu_info::numeric * 100::numeric / NULLIF((incidents_info.count_without_id_ern + incidents_info.count_discrepancy_epgu_info)::numeric,0)),0) AS percent_discrepancy_epgu_info
			   FROM actualization.incidents_info;

			ALTER TABLE actualization.view_incidents_epgu_info OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW actualization.view_info_sent_to_lk_epgu
			AS SELECT info_sent_to_lk_epgu.info_sent_to_lk_epgu_id,
				coalesce(round(info_sent_to_lk_epgu.count_status_formed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_formed,
				coalesce(round(info_sent_to_lk_epgu.count_status_not_formed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_not_formed,
				coalesce(round(info_sent_to_lk_epgu.count_sended::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_sended,
				coalesce(round(info_sent_to_lk_epgu.count_delivered::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_delivered,
				coalesce(round(info_sent_to_lk_epgu.count_error::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_error,
				coalesce(round(info_sent_to_lk_epgu.count_viewed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_viewed,
				coalesce(round(info_sent_to_lk_epgu.count_not_viewed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_not_viewed,
				info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed AS count_all
			   FROM actualization.info_sent_to_lk_epgu;

			ALTER TABLE actualization.view_incidents_epgu_info OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
	
	<changeSet id="0004" author="saliakhov">
        <comment>edit view admin_indicators</comment>
		<sql>
			CREATE OR REPLACE VIEW admin_indicators.view_user_analysis
			AS SELECT user_analysis.user_analysis_id,
				user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition AS count_all,
				coalesce(round(user_analysis.count_administrator_is::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_administrator_is,
				coalesce(round(user_analysis.count_administrator_poib::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_administrator_poib,
				coalesce(round(user_analysis.count_employee_gomy::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_employee_gomy,
				coalesce(round(user_analysis.count_observer_gomy::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_bserver_gomy,
				coalesce(round(user_analysis.count_supervisor_gomy::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_supervisor_gomy,
				coalesce(round(user_analysis.count_military_commissar::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_military_commissar,
				coalesce(round(user_analysis.count_specialist_statements::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_specialist_statements,
				coalesce(round(user_analysis.count_observer_vo::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_observer_vo,
				coalesce(round(user_analysis.count_observer_vk::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_observer_vk,
				coalesce(round(user_analysis.count_administrator_military_office::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_administrator_military_office,
				coalesce(round(user_analysis.count_specialist_military_accounting::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_specialist_military_accounting,
				coalesce(round(user_analysis.count_specialist_acquisition::numeric * 100::numeric / NULLIF((user_analysis.count_administrator_is + user_analysis.count_administrator_poib + user_analysis.count_employee_gomy + user_analysis.count_observer_gomy + user_analysis.count_supervisor_gomy + user_analysis.count_military_commissar + user_analysis.count_specialist_statements + user_analysis.count_observer_vo + user_analysis.count_observer_vk + user_analysis.count_administrator_military_office + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric,0)),0) AS percent_specialist_acquisition
			   FROM admin_indicators.user_analysis;

			ALTER TABLE admin_indicators.view_user_analysis OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
	
	<changeSet id="0005" author="saliakhov">
        <comment>edit view deregistration</comment>
		<sql>
			CREATE OR REPLACE VIEW deregistration.view_applications_received_from_epgu
			AS SELECT applications_received_from_epgu.applications_received_from_epgu_id,
				coalesce(round(applications_received_from_epgu.count_executor_appointed::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_appointed,
				coalesce(round(applications_received_from_epgu.count_executor_not_appointed_expired::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_not_appointed_expired,
				coalesce(round(applications_received_from_epgu.count_executor_not_appointed_on_time::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_not_appointed_on_time,
				coalesce(round(applications_received_from_epgu.count_registered::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_registered,
				coalesce(round(applications_received_from_epgu.count_registration_refusal::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_registration_refusal,
				coalesce(round(applications_received_from_epgu.count_refusal_provide_service::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_refusal_provide_service,
				coalesce(round(applications_received_from_epgu.count_provided::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_provided
			   FROM deregistration.applications_received_from_epgu;

			ALTER TABLE deregistration.view_applications_received_from_epgu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW deregistration.view_applications_sent_from_epgu
			AS SELECT applications_sent_from_epgu.applications_sent_from_epgu_id,
				applications_sent_from_epgu.count_moved_residence + applications_sent_from_epgu.count_departure_more_6_month + applications_sent_from_epgu.count_health_condition + applications_sent_from_epgu.count_age_limit AS count_all,
				coalesce(round(applications_sent_from_epgu.count_moved_residence::numeric * 100::numeric / NULLIF((applications_sent_from_epgu.count_moved_residence + applications_sent_from_epgu.count_departure_more_6_month + applications_sent_from_epgu.count_health_condition + applications_sent_from_epgu.count_age_limit)::numeric,0)),0) AS percent_moved_residence,
				coalesce(round(applications_sent_from_epgu.count_departure_more_6_month::numeric * 100::numeric / NULLIF((applications_sent_from_epgu.count_moved_residence + applications_sent_from_epgu.count_departure_more_6_month + applications_sent_from_epgu.count_health_condition + applications_sent_from_epgu.count_age_limit)::numeric,0)),0) AS percent_departure_more_6_month,
				coalesce(round(applications_sent_from_epgu.count_health_condition::numeric * 100::numeric / NULLIF((applications_sent_from_epgu.count_moved_residence + applications_sent_from_epgu.count_departure_more_6_month + applications_sent_from_epgu.count_health_condition + applications_sent_from_epgu.count_age_limit)::numeric,0)),0) AS percent_health_condition,
				coalesce(round(applications_sent_from_epgu.count_age_limit::numeric * 100::numeric / NULLIF((applications_sent_from_epgu.count_moved_residence + applications_sent_from_epgu.count_departure_more_6_month + applications_sent_from_epgu.count_health_condition + applications_sent_from_epgu.count_age_limit)::numeric,0)),0) AS percent_age_limit
			   FROM deregistration.applications_sent_from_epgu;

			ALTER TABLE deregistration.view_applications_sent_from_epgu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW deregistration.view_data_from_gir_vu
			AS SELECT data_from_gir_vu.data_from_gir_vu_id,
				data_from_gir_vu.count_arrived_death_reason + data_from_gir_vu.count_arrived_age_limit AS count_arrived_all,
				coalesce(round(data_from_gir_vu.count_arrived_death_reason::numeric * 100::numeric / NULLIF((data_from_gir_vu.count_arrived_death_reason + data_from_gir_vu.count_arrived_age_limit)::numeric,0)),0) AS percent_arrived_death_reason,
				coalesce(round(data_from_gir_vu.count_arrived_age_limit::numeric * 100::numeric / NULLIF((data_from_gir_vu.count_arrived_death_reason + data_from_gir_vu.count_arrived_age_limit)::numeric,0)),0) AS percent_arrived_age_limit
			   FROM deregistration.data_from_gir_vu;

			ALTER TABLE deregistration.view_data_from_gir_vu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW deregistration.view_decisions_signing
			AS SELECT decisions_signing.decisions_signing_id,
				decisions_signing.count_arrived_death_reason + decisions_signing.count_arrived_age_limit AS count_arrived_all,
				coalesce(round(decisions_signing.count_arrived_death_reason::numeric * 100::numeric / NULLIF((decisions_signing.count_arrived_death_reason + decisions_signing.count_arrived_age_limit)::numeric,0)),0) AS percent_arrived_death_reason,
				coalesce(round(decisions_signing.count_arrived_age_limit::numeric * 100::numeric / NULLIF((decisions_signing.count_arrived_death_reason + decisions_signing.count_arrived_age_limit)::numeric,0)),0) AS percent_arrived_age_limit
			   FROM deregistration.decisions_signing;

			ALTER TABLE deregistration.view_decisions_signing OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW deregistration.view_info_sent_to_lk_epgu
			AS SELECT info_sent_to_lk_epgu.info_sent_to_lk_epgu_id,
				coalesce(round(info_sent_to_lk_epgu.count_status_formed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_formed,
				coalesce(round(info_sent_to_lk_epgu.count_status_not_formed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_not_formed,
				coalesce(round(info_sent_to_lk_epgu.count_sended::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_sended,
				coalesce(round(info_sent_to_lk_epgu.count_delivered::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_delivered,
				coalesce(round(info_sent_to_lk_epgu.count_error::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_error,
				coalesce(round(info_sent_to_lk_epgu.count_viewed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_viewed,
				coalesce(round(info_sent_to_lk_epgu.count_not_viewed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_not_viewed,
				info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed AS count_all
			   FROM deregistration.info_sent_to_lk_epgu;

			ALTER TABLE deregistration.view_info_sent_to_lk_epgu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW deregistration.view_rejection_decisions
			AS SELECT rejection_decisions.rejection_decisions_id,
				rejection_decisions.count_for_sign + rejection_decisions.count_signed + rejection_decisions.count_refused AS count_all,
				coalesce(round(rejection_decisions.count_for_sign::numeric * 100::numeric / NULLIF((rejection_decisions.count_for_sign + rejection_decisions.count_signed + rejection_decisions.count_refused)::numeric,0)),0) AS percent_for_sign,
				coalesce(round(rejection_decisions.count_signed::numeric * 100::numeric / NULLIF((rejection_decisions.count_for_sign + rejection_decisions.count_signed + rejection_decisions.count_refused)::numeric,0)),0) AS percent_signed,
				coalesce(round(rejection_decisions.count_refused::numeric * 100::numeric / NULLIF((rejection_decisions.count_for_sign + rejection_decisions.count_signed + rejection_decisions.count_refused)::numeric,0)),0) AS percent_refused
			   FROM deregistration.rejection_decisions;

			ALTER TABLE deregistration.view_rejection_decisions OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW deregistration.view_removal_registration
			AS SELECT removal_registration.removal_registration_id,
				removal_registration.count_age_limit + removal_registration.count_reason_death + removal_registration.count_loss_citizenship + removal_registration.count_cancellation_record AS count_all,
				coalesce(round(removal_registration.count_age_limit::numeric * 100::numeric / NULLIF((removal_registration.count_age_limit + removal_registration.count_reason_death + removal_registration.count_loss_citizenship + removal_registration.count_cancellation_record)::numeric,0)),0) AS percent_age_limit,
				coalesce(round(removal_registration.count_reason_death::numeric * 100::numeric / NULLIF((removal_registration.count_age_limit + removal_registration.count_reason_death + removal_registration.count_loss_citizenship + removal_registration.count_cancellation_record)::numeric,0)),0) AS percent_reason_death,
				coalesce(round(removal_registration.count_loss_citizenship::numeric * 100::numeric / NULLIF((removal_registration.count_age_limit + removal_registration.count_reason_death + removal_registration.count_loss_citizenship + removal_registration.count_cancellation_record)::numeric,0)),0) AS percent_loss_citizenship,
				coalesce(round(removal_registration.count_cancellation_record::numeric * 100::numeric / NULLIF((removal_registration.count_age_limit + removal_registration.count_reason_death + removal_registration.count_loss_citizenship + removal_registration.count_cancellation_record)::numeric,0)),0) AS percent_cancellation_record
			   FROM deregistration.removal_registration;

			ALTER TABLE deregistration.view_removal_registration OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW deregistration.view_solutions
			AS SELECT solutions.solutions_id,
				solutions.count_accepted_signed + solutions.count_accepted_for_sign + solutions.count_refused AS count_accepted_all,
				coalesce(round(solutions.count_accepted_signed::numeric * 100::numeric / NULLIF((solutions.count_accepted_signed + solutions.count_accepted_for_sign + solutions.count_refused)::numeric,0)),0) AS percent_accepted_for_sign,
				coalesce(round(solutions.count_accepted_for_sign::numeric * 100::numeric / NULLIF((solutions.count_accepted_signed + solutions.count_accepted_for_sign + solutions.count_refused)::numeric,0)),0) AS percent_accepted_signed,
				coalesce(round(solutions.count_refused::numeric * 100::numeric / NULLIF((solutions.count_accepted_signed + solutions.count_accepted_for_sign + solutions.count_refused)::numeric,0)),0) AS percent_refused
			   FROM deregistration.solutions;

			ALTER TABLE deregistration.view_solutions OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
	
	<changeSet id="0006" author="saliakhov">
        <comment>edit view init_registration_info</comment>
		<sql>
			CREATE OR REPLACE VIEW init_registration_info.view_applications_received_from_epgu
			AS SELECT applications_received_from_epgu.applications_received_from_epgu_id,
				coalesce(round(applications_received_from_epgu.count_executor_appointed::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_appointed,
				coalesce(round(applications_received_from_epgu.count_registered::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_registered,
				coalesce(round(applications_received_from_epgu.count_registration_refusal::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_registration_refusal,
				coalesce(round(applications_received_from_epgu.count_refusal_provide_service::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_refusal_provide_service,
				coalesce(round(applications_received_from_epgu.count_executor_not_appointed_expired::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_not_appointed_expired,
				coalesce(round(applications_received_from_epgu.count_executor_not_appointed_on_time::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_not_appointed_on_time,
				coalesce(round(applications_received_from_epgu.count_received::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_received,
				coalesce(round(applications_received_from_epgu.count_provided::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_provided
			   FROM init_registration_info.applications_received_from_epgu;

			ALTER TABLE init_registration_info.view_applications_received_from_epgu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW init_registration_info.view_decision_formation_status
			AS SELECT decision_formation_status.decision_formation_status_id,
				decision_formation_status.count_signed + decision_formation_status.count_suggested + decision_formation_status.count_waiting_sign + decision_formation_status.count_refused AS count_all,
				coalesce(round(decision_formation_status.count_signed::numeric * 100::numeric / NULLIF((decision_formation_status.count_signed + decision_formation_status.count_suggested + decision_formation_status.count_waiting_sign + decision_formation_status.count_refused)::numeric,0)),0) AS percent_signed,
				coalesce(round(decision_formation_status.count_suggested::numeric * 100::numeric / NULLIF((decision_formation_status.count_signed + decision_formation_status.count_suggested + decision_formation_status.count_waiting_sign + decision_formation_status.count_refused)::numeric,0)),0) AS percent_suggested,
				coalesce(round(decision_formation_status.count_waiting_sign::numeric * 100::numeric / NULLIF((decision_formation_status.count_signed + decision_formation_status.count_suggested + decision_formation_status.count_waiting_sign + decision_formation_status.count_refused)::numeric,0)),0) AS percent_waiting_sign,
				coalesce(round(decision_formation_status.count_refused::numeric * 100::numeric / NULLIF((decision_formation_status.count_signed + decision_formation_status.count_suggested + decision_formation_status.count_waiting_sign + decision_formation_status.count_refused)::numeric,0)),0) AS percent_refused
			   FROM init_registration_info.decision_formation_status;

			ALTER TABLE init_registration_info.view_decision_formation_status OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW init_registration_info.view_incidents_info
			AS SELECT incidents_info.incidents_info_id,
				incidents_info.count_without_id_ern + incidents_info.count_discrepancy_epgu_info AS count_all,
				coalesce(round(incidents_info.count_without_id_ern::numeric * 100::numeric / NULLIF((incidents_info.count_without_id_ern + incidents_info.count_discrepancy_epgu_info)::numeric,0)),0) AS percent_without_id_ern,
				coalesce(round(incidents_info.count_discrepancy_epgu_info::numeric * 100::numeric / NULLIF((incidents_info.count_without_id_ern + incidents_info.count_discrepancy_epgu_info)::numeric,0)),0) AS percent_discrepancy_epgu_info
			   FROM init_registration_info.incidents_info;

			ALTER TABLE init_registration_info.view_incidents_info OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW init_registration_info.view_info_sent_to_lk_epgu
			AS SELECT info_sent_to_lk_epgu.info_sent_to_lk_epgu_id,
				coalesce(round(info_sent_to_lk_epgu.count_status_formed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_formed,
				coalesce(round(info_sent_to_lk_epgu.count_status_not_formed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_not_formed,
				coalesce(round(info_sent_to_lk_epgu.count_sended::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_sended,
				coalesce(round(info_sent_to_lk_epgu.count_delivered::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_delivered,
				coalesce(round(info_sent_to_lk_epgu.count_error::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_error,
				coalesce(round(info_sent_to_lk_epgu.count_viewed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_viewed,
				coalesce(round(info_sent_to_lk_epgu.count_not_viewed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_not_viewed,
				info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed AS count_all
			   FROM init_registration_info.info_sent_to_lk_epgu;

			ALTER TABLE init_registration_info.view_info_sent_to_lk_epgu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW init_registration_info.view_init_registration_from_gir_vu
			AS SELECT init_registration_from_gir_vu.init_registration_from_gir_vu_id,
				init_registration_from_gir_vu.count_womens + init_registration_from_gir_vu.count_received_citizenship + init_registration_from_gir_vu.count_serving_sentence AS count_all,
				coalesce(round(init_registration_from_gir_vu.count_womens::numeric * 100::numeric / NULLIF((init_registration_from_gir_vu.count_womens + init_registration_from_gir_vu.count_received_citizenship + init_registration_from_gir_vu.count_serving_sentence)::numeric,0)),0) AS percent_womens,
				coalesce(round(init_registration_from_gir_vu.count_received_citizenship::numeric * 100::numeric / NULLIF((init_registration_from_gir_vu.count_womens + init_registration_from_gir_vu.count_received_citizenship + init_registration_from_gir_vu.count_serving_sentence)::numeric,0)),0) AS percent_received_citizenship,
				coalesce(round(init_registration_from_gir_vu.count_serving_sentence::numeric * 100::numeric / NULLIF((init_registration_from_gir_vu.count_womens + init_registration_from_gir_vu.count_received_citizenship + init_registration_from_gir_vu.count_serving_sentence)::numeric,0)),0) AS percent_serving_sentence
			   FROM init_registration_info.init_registration_from_gir_vu;

			ALTER TABLE init_registration_info.view_init_registration_from_gir_vu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW init_registration_info.view_records_info_id_uk_id_ern
			AS SELECT records_info_id_uk_id_ern.records_info_id_uk_id_ern_id,
				records_info_id_uk_id_ern.records_with_id_uk + records_info_id_uk_id_ern.records_without_id_uk AS count_all,
				coalesce(round(records_info_id_uk_id_ern.records_with_id_uk::numeric * 100::numeric / NULLIF((records_info_id_uk_id_ern.records_with_id_uk + records_info_id_uk_id_ern.records_without_id_uk)::numeric,0)),0) AS percent_with_id_uk,
				coalesce(round(records_info_id_uk_id_ern.records_without_id_uk::numeric * 100::numeric / NULLIF((records_info_id_uk_id_ern.records_with_id_uk + records_info_id_uk_id_ern.records_without_id_uk)::numeric,0)),0) AS percent_without_id_uk
			   FROM init_registration_info.records_info_id_uk_id_ern;

			ALTER TABLE init_registration_info.view_records_info_id_uk_id_ern OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW init_registration_info.view_statuses_decisions_on_epgu
			AS SELECT statuses_decisions_on_epgu.statuses_decisions_on_epgu_id,
				coalesce(round(statuses_decisions_on_epgu.count_signed::numeric * 100::numeric / NULLIF((statuses_decisions_on_epgu.count_loaded)::numeric,0)),0) AS percent_signed,
				coalesce(round(statuses_decisions_on_epgu.count_formed_for_signing::numeric * 100::numeric / NULLIF((statuses_decisions_on_epgu.count_loaded)::numeric,0)),0) AS percent_formed_for_signing
			   FROM init_registration_info.statuses_decisions_on_epgu;

			ALTER TABLE init_registration_info.view_statuses_decisions_on_epgu OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
	
	<changeSet id="0007" author="saliakhov">
        <comment>edit view metrics</comment>
		<sql>
			CREATE OR REPLACE VIEW metrics.view_citizen_appeals
			AS SELECT citizen_appeals.citizen_appeals_id,
				citizen_appeals.count_satisfided + citizen_appeals.count_not_satisfided + citizen_appeals.count_accepted_expired + citizen_appeals.count_accepted_on_time AS count_accepted_to_consideration,
				citizen_appeals.count_submitted - (citizen_appeals.count_satisfided + citizen_appeals.count_not_satisfided + citizen_appeals.count_accepted_expired + citizen_appeals.count_accepted_on_time) AS count_submitted_difference_accepted,
				coalesce(round((citizen_appeals.count_satisfided + citizen_appeals.count_not_satisfided + citizen_appeals.count_accepted_expired + citizen_appeals.count_accepted_on_time)::numeric * 100::numeric / NULLIF((citizen_appeals.count_submitted)::numeric,0)),0) AS percent_accepted_to_consideration,
				coalesce(round(citizen_appeals.count_satisfided::numeric * 100::numeric / NULLIF((citizen_appeals.count_satisfided + citizen_appeals.count_not_satisfided + citizen_appeals.count_accepted_expired + citizen_appeals.count_accepted_on_time)::numeric,0)),0) AS percent_satisfided,
				coalesce(round(citizen_appeals.count_not_satisfided::numeric * 100::numeric / NULLIF((citizen_appeals.count_satisfided + citizen_appeals.count_not_satisfided + citizen_appeals.count_accepted_expired + citizen_appeals.count_accepted_on_time)::numeric,0)),0) AS percent_not_satisfided,
				coalesce(round(citizen_appeals.count_accepted_expired::numeric * 100::numeric / NULLIF((citizen_appeals.count_satisfided + citizen_appeals.count_not_satisfided + citizen_appeals.count_accepted_expired + citizen_appeals.count_accepted_on_time)::numeric,0)),0) AS percent_accepted_expired,
				coalesce(round(citizen_appeals.count_accepted_on_time::numeric * 100::numeric / NULLIF((citizen_appeals.count_satisfided + citizen_appeals.count_not_satisfided + citizen_appeals.count_accepted_expired + citizen_appeals.count_accepted_on_time)::numeric,0)),0) AS percent_accepted_on_time
			   FROM metrics.citizen_appeals;

			ALTER TABLE metrics.view_citizen_appeals OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_convert_info_comparison_csv_xml
			AS SELECT convert_info_comparison_csv_xml.convert_info_comparison_csv_xml_id,
				coalesce(round(convert_info_comparison_csv_xml.count_csv_formed::numeric * 100::numeric / NULLIF((convert_info_comparison_csv_xml.count_xml_formed)::numeric,0)),0) AS percent_csv_formed
			   FROM metrics.convert_info_comparison_csv_xml;

			ALTER TABLE metrics.view_convert_info_comparison_csv_xml OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_convert_info_loaded_records
			AS SELECT convert_info_loaded_records.convert_info_loaded_records_id,
				convert_info_loaded_records.count_success + convert_info_loaded_records.count_error + convert_info_loaded_records.count_not_success AS count_all,
				coalesce(round(convert_info_loaded_records.count_error::numeric * 100::numeric / NULLIF((convert_info_loaded_records.count_success + convert_info_loaded_records.count_error + convert_info_loaded_records.count_not_success)::numeric,0)),0) AS percent_error,
				coalesce(round(convert_info_loaded_records.count_success::numeric * 100::numeric / NULLIF((convert_info_loaded_records.count_success + convert_info_loaded_records.count_error + convert_info_loaded_records.count_not_success)::numeric,0)),0) AS percent_success,
				coalesce(round(convert_info_loaded_records.count_not_success::numeric * 100::numeric / NULLIF((convert_info_loaded_records.count_success + convert_info_loaded_records.count_error + convert_info_loaded_records.count_not_success)::numeric,0)),0) AS percent_not_success
			   FROM metrics.convert_info_loaded_records;

			ALTER TABLE metrics.view_convert_info_loaded_records OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_convert_info_not_identifited_records
			AS SELECT convert_info_not_identifited_records.convert_info_not_identifited_records_id,
				convert_info_not_identifited_records.count_not_in_csv + convert_info_not_identifited_records.count_not_in_xml AS count_all,
				coalesce(round(convert_info_not_identifited_records.count_not_in_xml::numeric * 100::numeric / NULLIF((convert_info_not_identifited_records.count_not_in_csv + convert_info_not_identifited_records.count_not_in_xml)::numeric,0)),0) AS percent_not_in_xml,
				coalesce(round(convert_info_not_identifited_records.count_not_in_csv::numeric * 100::numeric / NULLIF((convert_info_not_identifited_records.count_not_in_csv + convert_info_not_identifited_records.count_not_in_xml)::numeric,0)),0) AS percent_not_in_csv
			   FROM metrics.convert_info_not_identifited_records;

			ALTER TABLE metrics.view_convert_info_not_identifited_records OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_convert_info_records_from_easu
			AS SELECT convert_info_records_from_easu.convert_info_records_from_easu_id,
				convert_info_records_from_easu.count_completed_record + convert_info_records_from_easu.count_flk_and_duplicate AS count_all,
				coalesce(round(convert_info_records_from_easu.count_completed_record::numeric * 100::numeric / NULLIF((convert_info_records_from_easu.count_completed_record + convert_info_records_from_easu.count_flk_and_duplicate)::numeric,0)),0) AS percent_completed_record,
				coalesce(round(convert_info_records_from_easu.count_flk_and_duplicate::numeric * 100::numeric / NULLIF((convert_info_records_from_easu.count_completed_record + convert_info_records_from_easu.count_flk_and_duplicate)::numeric,0)),0) AS percent_flk_and_duplicate
			   FROM metrics.convert_info_records_from_easu;

			ALTER TABLE metrics.view_convert_info_records_from_easu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_convert_info_sent_to_lk_epgu
			AS SELECT convert_info_sent_to_lk_epgu.convert_info_sent_to_lk_epgu_id,
				coalesce(round(convert_info_sent_to_lk_epgu.count_status_formed::numeric * 100::numeric / NULLIF((convert_info_sent_to_lk_epgu.count_status_formed + convert_info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_formed,
				coalesce(round(convert_info_sent_to_lk_epgu.count_status_not_formed::numeric * 100::numeric / NULLIF((convert_info_sent_to_lk_epgu.count_status_formed + convert_info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_not_formed,
				coalesce(round(convert_info_sent_to_lk_epgu.count_sended::numeric * 100::numeric / NULLIF((convert_info_sent_to_lk_epgu.count_status_formed + convert_info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_sended,
				coalesce(round(convert_info_sent_to_lk_epgu.count_delivered::numeric * 100::numeric / NULLIF((convert_info_sent_to_lk_epgu.count_status_formed + convert_info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_delivered,
				coalesce(round(convert_info_sent_to_lk_epgu.count_error::numeric * 100::numeric / NULLIF((convert_info_sent_to_lk_epgu.count_status_formed + convert_info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_error,
				coalesce(round(convert_info_sent_to_lk_epgu.count_viewed::numeric * 100::numeric / NULLIF((convert_info_sent_to_lk_epgu.count_status_formed + convert_info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_viewed,
				coalesce(round(convert_info_sent_to_lk_epgu.count_not_viewed::numeric * 100::numeric / NULLIF((convert_info_sent_to_lk_epgu.count_status_formed + convert_info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_not_viewed,
				convert_info_sent_to_lk_epgu.count_status_formed + convert_info_sent_to_lk_epgu.count_status_not_formed AS count_all
			   FROM metrics.convert_info_sent_to_lk_epgu;

			ALTER TABLE metrics.view_convert_info_sent_to_lk_epgu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_criminal_administrative_liability
			AS SELECT criminal_administrative_liability.criminal_administrative_liability_id,
				coalesce(round(criminal_administrative_liability.count_criminal::numeric * 100::numeric / NULLIF((criminal_administrative_liability.count_all)::numeric,0)),0) AS percent_criminal,
				coalesce(round(criminal_administrative_liability.count_administrative::numeric * 100::numeric / NULLIF((criminal_administrative_liability.count_all)::numeric,0)),0) AS percent_administrative,
				criminal_administrative_liability.count_all - criminal_administrative_liability.count_criminal AS count_without_criminal,
				criminal_administrative_liability.count_all - criminal_administrative_liability.count_administrative AS count_without_administrative
			   FROM metrics.criminal_administrative_liability;

			ALTER TABLE metrics.view_criminal_administrative_liability OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_fz_53_appeals
			AS SELECT fz_53_appeals.fz_53_appeals_id,
				fz_53_appeals.count_accepted + fz_53_appeals.count_refused + fz_53_appeals.count_partially AS count_all,
				coalesce(round(fz_53_appeals.count_accepted::numeric * 100::numeric / NULLIF((fz_53_appeals.count_accepted + fz_53_appeals.count_refused + fz_53_appeals.count_partially)::numeric,0)),0) AS percent_accepted,
				coalesce(round(fz_53_appeals.count_refused::numeric * 100::numeric / NULLIF((fz_53_appeals.count_accepted + fz_53_appeals.count_refused + fz_53_appeals.count_partially)::numeric,0)),0) AS percent_refused,
				coalesce(round(fz_53_appeals.count_partially::numeric * 100::numeric / NULLIF((fz_53_appeals.count_accepted + fz_53_appeals.count_refused + fz_53_appeals.count_partially)::numeric,0)),0) AS percent_partially
			   FROM metrics.fz_53_appeals;

			ALTER TABLE metrics.view_fz_53_appeals OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_incidents_change_data_from_gir_vu
			AS SELECT incidents_change_data_from_gir_vu.incidents_change_data_from_gir_vu_id,
				incidents_change_data_from_gir_vu.count_epgu_citizen_appeal + incidents_change_data_from_gir_vu.count_manual + incidents_change_data_from_gir_vu.count_send_to_gir_vu AS count_all,
				coalesce(round(incidents_change_data_from_gir_vu.count_epgu_citizen_appeal::numeric * 100::numeric / NULLIF((incidents_change_data_from_gir_vu.count_epgu_citizen_appeal + incidents_change_data_from_gir_vu.count_manual + incidents_change_data_from_gir_vu.count_send_to_gir_vu)::numeric,0)),0) AS percent_epgu_citizen_appeal,
				coalesce(round(incidents_change_data_from_gir_vu.count_manual::numeric * 100::numeric / NULLIF((incidents_change_data_from_gir_vu.count_epgu_citizen_appeal + incidents_change_data_from_gir_vu.count_manual + incidents_change_data_from_gir_vu.count_send_to_gir_vu)::numeric,0)),0) AS percent_manual,
				coalesce(round(incidents_change_data_from_gir_vu.count_send_to_gir_vu::numeric * 100::numeric / NULLIF((incidents_change_data_from_gir_vu.count_epgu_citizen_appeal + incidents_change_data_from_gir_vu.count_manual + incidents_change_data_from_gir_vu.count_send_to_gir_vu)::numeric,0)),0) AS percent_send_to_gir_vu
			   FROM metrics.incidents_change_data_from_gir_vu;

			ALTER TABLE metrics.view_incidents_change_data_from_gir_vu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_incidents_closed
			AS SELECT incidents_closed.incidents_closed_id,
				incidents_closed.count_refused_to_archive + incidents_closed.count_accepted_to_archive + incidents_closed.count_auto_closed + incidents_closed.count_manual_closed + incidents_closed.count_returned AS count_all,
				coalesce(round(incidents_closed.count_refused_to_archive::numeric * 100::numeric / NULLIF((incidents_closed.count_refused_to_archive + incidents_closed.count_accepted_to_archive + incidents_closed.count_auto_closed + incidents_closed.count_manual_closed + incidents_closed.count_returned)::numeric,0)),0) AS percent_refused_to_archive,
				coalesce(round(incidents_closed.count_accepted_to_archive::numeric * 100::numeric / NULLIF((incidents_closed.count_refused_to_archive + incidents_closed.count_accepted_to_archive + incidents_closed.count_auto_closed + incidents_closed.count_manual_closed + incidents_closed.count_returned)::numeric,0)),0) AS percent_accepted_to_archive,
				coalesce(round(incidents_closed.count_auto_closed::numeric * 100::numeric / NULLIF((incidents_closed.count_refused_to_archive + incidents_closed.count_accepted_to_archive + incidents_closed.count_auto_closed + incidents_closed.count_manual_closed + incidents_closed.count_returned)::numeric,0)),0) AS percent_auto_closed,
				coalesce(round(incidents_closed.count_manual_closed::numeric * 100::numeric / NULLIF((incidents_closed.count_refused_to_archive + incidents_closed.count_accepted_to_archive + incidents_closed.count_auto_closed + incidents_closed.count_manual_closed + incidents_closed.count_returned)::numeric,0)),0) AS percent_manual_closed,
				coalesce(round(incidents_closed.count_returned::numeric * 100::numeric / NULLIF((incidents_closed.count_refused_to_archive + incidents_closed.count_accepted_to_archive + incidents_closed.count_auto_closed + incidents_closed.count_manual_closed + incidents_closed.count_returned)::numeric,0)),0) AS percent_returned
			   FROM metrics.incidents_closed;

			ALTER TABLE metrics.view_incidents_closed OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_incidents_files_requests
			AS SELECT incidents_files_requests.incidents_files_requests_id,
				coalesce(round(incidents_files_requests.count_requisites_entered::numeric * 100::numeric / NULLIF((incidents_files_requests.count_requisites_entered)::numeric,0)),0) AS percent_requisites_entered,
				coalesce(round(incidents_files_requests.count_results_entered::numeric * 100::numeric / NULLIF((incidents_files_requests.count_requisites_entered)::numeric,0)),0) AS percent_results_entered
			   FROM metrics.incidents_files_requests;

			ALTER TABLE metrics.view_incidents_files_requests OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW metrics.view_incidents_recieved_by_data_from_gir_vu
			AS SELECT incidents_recieved_by_data_from_gir_vu.incidents_recieved_by_data_from_gir_vu_id,
				incidents_recieved_by_data_from_gir_vu.count_confirmed + incidents_recieved_by_data_from_gir_vu.count_not_confirmed AS count_all,
				coalesce(round(incidents_recieved_by_data_from_gir_vu.count_confirmed::numeric * 100::numeric / NULLIF((incidents_recieved_by_data_from_gir_vu.count_confirmed + incidents_recieved_by_data_from_gir_vu.count_not_confirmed)::numeric,0)),0) AS percent_confirmed,
				coalesce(round(incidents_recieved_by_data_from_gir_vu.count_not_confirmed::numeric * 100::numeric / NULLIF((incidents_recieved_by_data_from_gir_vu.count_confirmed + incidents_recieved_by_data_from_gir_vu.count_not_confirmed)::numeric,0)),0) AS percent_not_confirmed
			   FROM metrics.incidents_recieved_by_data_from_gir_vu;

			ALTER TABLE metrics.view_incidents_recieved_by_data_from_gir_vu OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
	
	<changeSet id="0008" author="saliakhov">
        <comment>edit view registration_change_address</comment>
		<sql>
			CREATE OR REPLACE VIEW registration_change_address.view_applications_received_from_epgu
			AS SELECT applications_received_from_epgu.applications_received_from_epgu_id,
				coalesce(round(applications_received_from_epgu.count_executor_appointed::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_appointed,
				coalesce(round(applications_received_from_epgu.count_executor_not_appointed_expired::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_not_appointed_expired,
				coalesce(round(applications_received_from_epgu.count_executor_not_appointed_on_time::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_executor_not_appointed_on_time,
				coalesce(round(applications_received_from_epgu.count_registered::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_registered,
				coalesce(round(applications_received_from_epgu.count_registration_refusal::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_registration_refusal,
				coalesce(round(applications_received_from_epgu.count_refusal_provide_service::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_refusal_provide_service,
				coalesce(round(applications_received_from_epgu.count_provided::numeric * 100::numeric / NULLIF((applications_received_from_epgu.count_all)::numeric,0)),0) AS percent_provided
			   FROM registration_change_address.applications_received_from_epgu;

			ALTER TABLE registration_change_address.view_applications_received_from_epgu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW registration_change_address.view_change_data_from_gir_vu
			AS SELECT change_data_from_gir_vu.change_data_from_gir_vu_id,
				coalesce(round(change_data_from_gir_vu.count_requested_to_send::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_all)::numeric,0)),0) AS percent_requested_to_send,
				coalesce(round(change_data_from_gir_vu.count_accepted_to_send_from_first::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_all)::numeric,0)),0) AS percent_accepted_to_send_from_first,
				coalesce(round(change_data_from_gir_vu.count_denied_requests::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_all)::numeric,0)),0) AS percent_denied_requests,
				coalesce(round(change_data_from_gir_vu.count_solution_send_signed::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_all)::numeric,0)),0) AS percent_requested_solution_send_signed,
				coalesce(round(change_data_from_gir_vu.count_unloaded::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_all)::numeric,0)),0) AS percent_requested_unloaded,
				coalesce(round(change_data_from_gir_vu.count_solutions_registration_formed::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_all)::numeric,0)),0) AS percent_requested_solutions_formed,
				coalesce(round(change_data_from_gir_vu.count_solutions_registration_signed::numeric * 100::numeric / NULLIF((change_data_from_gir_vu.count_all)::numeric,0)),0) AS percent_solutions_registration_signed
			   FROM registration_change_address.change_data_from_gir_vu;

			ALTER TABLE registration_change_address.view_change_data_from_gir_vu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW registration_change_address.view_change_data_personal_visit
			AS SELECT change_data_personal_visit.change_data_personal_visit_id,
				change_data_personal_visit.count_for_sign + change_data_personal_visit.count_signed + change_data_personal_visit.count_refused AS count_all,
				coalesce(round(change_data_personal_visit.count_for_sign::numeric * 100::numeric / NULLIF((change_data_personal_visit.count_for_sign + change_data_personal_visit.count_signed + change_data_personal_visit.count_refused)::numeric,0)),0) AS percent_for_sign,
				coalesce(round(change_data_personal_visit.count_signed::numeric * 100::numeric / NULLIF((change_data_personal_visit.count_for_sign + change_data_personal_visit.count_signed + change_data_personal_visit.count_refused)::numeric,0)),0) AS percent_signed,
				coalesce(round(change_data_personal_visit.count_refused::numeric * 100::numeric / NULLIF((change_data_personal_visit.count_for_sign + change_data_personal_visit.count_signed + change_data_personal_visit.count_refused)::numeric,0)),0) AS percent_refused
			   FROM registration_change_address.change_data_personal_visit;

			ALTER TABLE registration_change_address.view_change_data_personal_visit OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW registration_change_address.view_imposition_tmp_measures
			AS SELECT imposition_tmp_measures.imposition_tmp_measures_id,
				imposition_tmp_measures.count_refused_in_second + imposition_tmp_measures.count_accepted_in_second AS count_all,
				coalesce(round(imposition_tmp_measures.count_refused_in_second::numeric * 100::numeric / NULLIF((imposition_tmp_measures.count_refused_in_second + imposition_tmp_measures.count_accepted_in_second)::numeric,0)),0) AS percent_refused_in_second,
				coalesce(round(imposition_tmp_measures.count_accepted_in_second::numeric * 100::numeric / NULLIF((imposition_tmp_measures.count_refused_in_second + imposition_tmp_measures.count_accepted_in_second)::numeric,0)),0) AS percent_accepted_in_second
			   FROM registration_change_address.imposition_tmp_measures;

			ALTER TABLE registration_change_address.view_imposition_tmp_measures OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW registration_change_address.view_incidents_epgu_info
			AS SELECT incidents_epgu_info.incidents_epgu_info_id,
				coalesce(round(incidents_epgu_info.count_registered::numeric * 100::numeric / NULLIF((incidents_epgu_info.count_identify)::numeric,0)),0) AS percent_registered,
				incidents_epgu_info.count_identify - incidents_epgu_info.count_registered AS count_not_identify
			   FROM registration_change_address.incidents_epgu_info;

			ALTER TABLE registration_change_address.view_incidents_epgu_info OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW registration_change_address.view_incidents_info
			AS SELECT incidents_info.incidents_info_id,
				incidents_info.count_without_id_ern + incidents_info.count_discrepancy_epgu_info AS count_all,
				coalesce(round(incidents_info.count_without_id_ern::numeric * 100::numeric / NULLIF((incidents_info.count_without_id_ern + incidents_info.count_discrepancy_epgu_info)::numeric,0)),0) AS percent_without_id_ern,
				coalesce(round(incidents_info.count_discrepancy_epgu_info::numeric * 100::numeric / NULLIF((incidents_info.count_without_id_ern + incidents_info.count_discrepancy_epgu_info)::numeric,0)),0) AS percent_discrepancy_epgu_info
			   FROM registration_change_address.incidents_info;

			ALTER TABLE registration_change_address.view_incidents_info OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW registration_change_address.view_info_sent_to_lk_epgu
			AS SELECT info_sent_to_lk_epgu.info_sent_to_lk_epgu_id,
				coalesce(round(info_sent_to_lk_epgu.count_status_formed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_formed,
				coalesce(round(info_sent_to_lk_epgu.count_status_not_formed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_not_formed,
				coalesce(round(info_sent_to_lk_epgu.count_sended::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_sended,
				coalesce(round(info_sent_to_lk_epgu.count_delivered::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_delivered,
				coalesce(round(info_sent_to_lk_epgu.count_error::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_error,
				coalesce(round(info_sent_to_lk_epgu.count_viewed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_viewed,
				coalesce(round(info_sent_to_lk_epgu.count_not_viewed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_not_viewed,
				info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed AS count_all
			   FROM registration_change_address.info_sent_to_lk_epgu;

			ALTER TABLE registration_change_address.view_info_sent_to_lk_epgu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW registration_change_address.view_personal_info_stat
			AS SELECT personal_info_stat.personal_info_stat_id,
				personal_info_stat.count_refused + personal_info_stat.count_accepted_to_send AS count_all,
				coalesce(round(personal_info_stat.count_refused::numeric * 100::numeric / NULLIF((personal_info_stat.count_refused + personal_info_stat.count_accepted_to_send)::numeric,0)),0) AS percent_refused,
				coalesce(round(personal_info_stat.count_unloaded::numeric * 100::numeric / NULLIF((personal_info_stat.count_refused + personal_info_stat.count_accepted_to_send)::numeric,0)),0) AS percent_unloaded,
				coalesce(round(personal_info_stat.count_accepted_to_send::numeric * 100::numeric / NULLIF((personal_info_stat.count_refused + personal_info_stat.count_accepted_to_send)::numeric,0)),0) AS percent_accepted_to_send
			   FROM registration_change_address.personal_info_stat;

			ALTER TABLE registration_change_address.view_personal_info_stat OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW registration_change_address.view_records_about_registrated_citizen
			AS SELECT records_about_registrated_citizen.records_about_registrated_citizen_id,
				records_about_registrated_citizen.count_registrated_by_app_vk + records_about_registrated_citizen.count_registrated_another_vk AS count_all,
				coalesce(round(records_about_registrated_citizen.count_registrated_by_app_vk::numeric * 100::numeric / NULLIF((records_about_registrated_citizen.count_registrated_by_app_vk + records_about_registrated_citizen.count_registrated_another_vk)::numeric,0)),0) AS percent_registrated_by_app_vk,
				coalesce(round(records_about_registrated_citizen.count_registrated_another_vk::numeric * 100::numeric / NULLIF((records_about_registrated_citizen.count_registrated_by_app_vk + records_about_registrated_citizen.count_registrated_another_vk)::numeric,0)),0) AS percent_registrated_another_vk
			   FROM registration_change_address.records_about_registrated_citizen;

			ALTER TABLE registration_change_address.view_records_about_registrated_citizen OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW registration_change_address.view_summonses_sign
			AS SELECT summonses_sign.summonses_sign_id,
				summonses_sign.count_refused_in_second + summonses_sign.count_accepted_in_second AS count_all,
				coalesce(round(summonses_sign.count_refused_in_second::numeric * 100::numeric / NULLIF((summonses_sign.count_refused_in_second + summonses_sign.count_accepted_in_second)::numeric,0)),0) AS percent_refused_in_second,
				coalesce(round(summonses_sign.count_accepted_in_second::numeric * 100::numeric / NULLIF((summonses_sign.count_refused_in_second + summonses_sign.count_accepted_in_second)::numeric,0)),0) AS percent_accepted_in_second
			   FROM registration_change_address.summonses_sign;

			ALTER TABLE registration_change_address.view_summonses_sign OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
	
	<changeSet id="0009" author="saliakhov">
        <comment>edit view summonses_list</comment>
		<sql>
			CREATE OR REPLACE VIEW summonses_list.view_all_summonses
			AS SELECT all_summonses.all_summonses_id,
				all_summonses.count_init_registration + all_summonses.count_draft_company + all_summonses.count_management_military_registration + all_summonses.count_mobilization AS count_all,
				coalesce(round(all_summonses.count_init_registration::numeric * 100::numeric / NULLIF((all_summonses.count_init_registration + all_summonses.count_draft_company + all_summonses.count_management_military_registration + all_summonses.count_mobilization)::numeric,0)),0) AS percent_init_registration,
				coalesce(round(all_summonses.count_draft_company::numeric * 100::numeric / NULLIF((all_summonses.count_init_registration + all_summonses.count_draft_company + all_summonses.count_management_military_registration + all_summonses.count_mobilization)::numeric,0)),0) AS percent_draft_company,
				coalesce(round(all_summonses.count_management_military_registration::numeric * 100::numeric / NULLIF((all_summonses.count_init_registration + all_summonses.count_draft_company + all_summonses.count_management_military_registration + all_summonses.count_mobilization)::numeric,0)),0) AS percent_management_military_registration,
				coalesce(round(all_summonses.count_mobilization::numeric * 100::numeric / NULLIF((all_summonses.count_init_registration + all_summonses.count_draft_company + all_summonses.count_management_military_registration + all_summonses.count_mobilization)::numeric,0)),0) AS percent_mobilization
			   FROM summonses_list.all_summonses;

			ALTER TABLE summonses_list.view_all_summonses OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW summonses_list.view_formed_summonses
			AS SELECT formed_summonses.formed_summonses_id,
				formed_summonses.count_formed + formed_summonses.count_signed + formed_summonses.count_sent + formed_summonses.count_delivered_on_hands + formed_summonses.count_delivered_by_letter + formed_summonses.count_refused + formed_summonses.count_auto_7_days AS count_all,
				coalesce(round(formed_summonses.count_formed::numeric * 100::numeric / NULLIF((formed_summonses.count_formed + formed_summonses.count_signed + formed_summonses.count_sent + formed_summonses.count_delivered_on_hands + formed_summonses.count_delivered_by_letter + formed_summonses.count_refused + formed_summonses.count_auto_7_days)::numeric,0)),0) AS percent_formed,
				coalesce(round(formed_summonses.count_signed::numeric * 100::numeric / NULLIF((formed_summonses.count_formed + formed_summonses.count_signed + formed_summonses.count_sent + formed_summonses.count_delivered_on_hands + formed_summonses.count_delivered_by_letter + formed_summonses.count_refused + formed_summonses.count_auto_7_days)::numeric,0)),0) AS percent_signed,
				coalesce(round(formed_summonses.count_sent::numeric * 100::numeric / NULLIF((formed_summonses.count_formed + formed_summonses.count_signed + formed_summonses.count_sent + formed_summonses.count_delivered_on_hands + formed_summonses.count_delivered_by_letter + formed_summonses.count_refused + formed_summonses.count_auto_7_days)::numeric,0)),0) AS percent_sent,
				coalesce(round(formed_summonses.count_delivered_on_hands::numeric * 100::numeric / NULLIF((formed_summonses.count_formed + formed_summonses.count_signed + formed_summonses.count_sent + formed_summonses.count_delivered_on_hands + formed_summonses.count_delivered_by_letter + formed_summonses.count_refused + formed_summonses.count_auto_7_days)::numeric,0)),0) AS percent_delivered_on_hands,
				coalesce(round(formed_summonses.count_delivered_by_letter::numeric * 100::numeric / NULLIF((formed_summonses.count_formed + formed_summonses.count_signed + formed_summonses.count_sent + formed_summonses.count_delivered_on_hands + formed_summonses.count_delivered_by_letter + formed_summonses.count_refused + formed_summonses.count_auto_7_days)::numeric,0)),0) AS percent_delivered_by_letter,
				coalesce(round(formed_summonses.count_refused::numeric * 100::numeric / NULLIF((formed_summonses.count_formed + formed_summonses.count_signed + formed_summonses.count_sent + formed_summonses.count_delivered_on_hands + formed_summonses.count_delivered_by_letter + formed_summonses.count_refused + formed_summonses.count_auto_7_days)::numeric,0)),0) AS percent_refused,
				coalesce(round(formed_summonses.count_auto_7_days::numeric * 100::numeric / NULLIF((formed_summonses.count_formed + formed_summonses.count_signed + formed_summonses.count_sent + formed_summonses.count_delivered_on_hands + formed_summonses.count_delivered_by_letter + formed_summonses.count_refused + formed_summonses.count_auto_7_days)::numeric,0)),0) AS percent_auto_7_days
			   FROM summonses_list.formed_summonses;

			ALTER TABLE summonses_list.view_formed_summonses OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW summonses_list.view_imposition_tmp_measures
			AS SELECT imposition_tmp_measures.imposition_tmp_measures_id,
				imposition_tmp_measures.count_for_sign + imposition_tmp_measures.count_apply + imposition_tmp_measures.count_enter AS count_all,
				coalesce(round(imposition_tmp_measures.count_for_sign::numeric * 100::numeric / NULLIF((imposition_tmp_measures.count_for_sign + imposition_tmp_measures.count_apply + imposition_tmp_measures.count_enter)::numeric,0)),0) AS percent_for_sign,
				coalesce(round(imposition_tmp_measures.count_apply::numeric * 100::numeric / NULLIF((imposition_tmp_measures.count_for_sign + imposition_tmp_measures.count_apply + imposition_tmp_measures.count_enter)::numeric,0)),0) AS percent_apply,
				coalesce(round(imposition_tmp_measures.count_enter::numeric * 100::numeric / NULLIF((imposition_tmp_measures.count_for_sign + imposition_tmp_measures.count_apply + imposition_tmp_measures.count_enter)::numeric,0)),0) AS percent_enter
			   FROM summonses_list.imposition_tmp_measures;

			ALTER TABLE summonses_list.view_imposition_tmp_measures OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW summonses_list.view_info_sent_to_lk_epgu
			AS SELECT info_sent_to_lk_epgu.info_sent_to_lk_epgu_id,
				coalesce(round(info_sent_to_lk_epgu.count_status_formed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_formed,
				coalesce(round(info_sent_to_lk_epgu.count_status_not_formed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_status_not_formed,
				coalesce(round(info_sent_to_lk_epgu.count_sended::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_sended,
				coalesce(round(info_sent_to_lk_epgu.count_delivered::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_delivered,
				coalesce(round(info_sent_to_lk_epgu.count_error::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_error,
				coalesce(round(info_sent_to_lk_epgu.count_viewed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_viewed,
				coalesce(round(info_sent_to_lk_epgu.count_not_viewed::numeric * 100::numeric / NULLIF((info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed)::numeric,0)),0) AS percent_not_viewed,
				info_sent_to_lk_epgu.count_status_formed + info_sent_to_lk_epgu.count_status_not_formed AS count_all
			   FROM summonses_list.info_sent_to_lk_epgu;

			ALTER TABLE summonses_list.view_info_sent_to_lk_epgu OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW summonses_list.view_recruit_office_visit
			AS SELECT t.recruit_office_visit_id,
				t.count_all,
				coalesce(round(t.count_visit::numeric * 100::numeric / NULLIF((t.count_all)::numeric,0)),0) AS percent_visit,
				coalesce(round(t.count_not_visit::numeric * 100::numeric / NULLIF((t.count_all)::numeric,0)),0) AS percent_not_visit,
				coalesce(round(t.count_valid_reason::numeric * 100::numeric / NULLIF((t.count_all)::numeric,0)),0) AS percent_valid_reason,
				coalesce(round(t.count_not_need::numeric * 100::numeric / NULLIF((t.count_all)::numeric,0)),0) AS percent_not_need,
				coalesce(round(t.count_hand_over_not_need::numeric * 100::numeric / NULLIF((t.count_all)::numeric,0)),0) AS percent_hand_over_not_need
			   FROM ( SELECT recruit_office_visit.count_visit + recruit_office_visit.count_not_visit + recruit_office_visit.count_valid_reason + recruit_office_visit.count_not_need + recruit_office_visit.count_hand_over_not_need AS count_all,
						recruit_office_visit.recruit_office_visit_id,
						recruit_office_visit.recruitment_id,
						recruit_office_visit.update_date,
						recruit_office_visit.info_date,
						recruit_office_visit.summonses_reason_id,
						recruit_office_visit.count_visit,
						recruit_office_visit.count_not_visit,
						recruit_office_visit.count_valid_reason,
						recruit_office_visit.count_not_need,
						recruit_office_visit.count_hand_over_not_need
					   FROM summonses_list.recruit_office_visit) t;

			ALTER TABLE summonses_list.view_recruit_office_visit OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW summonses_list.view_remove_tmp_measures
			AS SELECT remove_tmp_measures.remove_tmp_measures_id,
				remove_tmp_measures.count_formed + remove_tmp_measures.count_for_sign + remove_tmp_measures.count_signed + remove_tmp_measures.count_removed AS count_all,
				coalesce(round(remove_tmp_measures.count_formed::numeric * 100::numeric / NULLIF((remove_tmp_measures.count_formed + remove_tmp_measures.count_for_sign + remove_tmp_measures.count_signed + remove_tmp_measures.count_removed)::numeric,0)),0) AS percent_formed,
				coalesce(round(remove_tmp_measures.count_for_sign::numeric * 100::numeric / NULLIF((remove_tmp_measures.count_formed + remove_tmp_measures.count_for_sign + remove_tmp_measures.count_signed + remove_tmp_measures.count_removed)::numeric,0)),0) AS percent_for_sign,
				coalesce(round(remove_tmp_measures.count_signed::numeric * 100::numeric / NULLIF((remove_tmp_measures.count_formed + remove_tmp_measures.count_for_sign + remove_tmp_measures.count_signed + remove_tmp_measures.count_removed)::numeric,0)),0) AS percent_signed,
				coalesce(round(remove_tmp_measures.count_removed::numeric * 100::numeric / NULLIF((remove_tmp_measures.count_formed + remove_tmp_measures.count_for_sign + remove_tmp_measures.count_signed + remove_tmp_measures.count_removed)::numeric,0)),0) AS percent_removed
			   FROM summonses_list.remove_tmp_measures;

			ALTER TABLE summonses_list.view_remove_tmp_measures OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
	
	<changeSet id="0010" author="saliakhov">
        <comment>edit view summonses_list_subsystem</comment>
		<sql>
			CREATE OR REPLACE VIEW summonses_list_subsystem.view_requests_direct_to_pers_acc
			AS SELECT requests_direct_to_pers_acc.requests_direct_to_pers_acc_id,
				requests_direct_to_pers_acc.count_registered + requests_direct_to_pers_acc.count_not_registered AS count_all,
				coalesce(round(requests_direct_to_pers_acc.count_registered::numeric * 100::numeric / NULLIF((requests_direct_to_pers_acc.count_registered + requests_direct_to_pers_acc.count_not_registered)::numeric,0)),0) AS percent_registered,
				coalesce(round(requests_direct_to_pers_acc.count_not_registered::numeric * 100::numeric / NULLIF((requests_direct_to_pers_acc.count_registered + requests_direct_to_pers_acc.count_not_registered)::numeric,0)),0) AS percent_not_registered,
				coalesce(round(requests_direct_to_pers_acc.count_registered_formed::numeric * 100::numeric / NULLIF((requests_direct_to_pers_acc.count_registered)::numeric,0)),0) AS percent_registered_formed,
				coalesce(round(requests_direct_to_pers_acc.count_not_registered_formed::numeric * 100::numeric / NULLIF((requests_direct_to_pers_acc.count_not_registered)::numeric,0)),0) AS percent_not_registered_formed
			   FROM summonses_list_subsystem.requests_direct_to_pers_acc;

			ALTER TABLE summonses_list_subsystem.view_requests_direct_to_pers_acc OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW summonses_list_subsystem.view_summonses_direct_to_pers_acc
			AS SELECT summonses_direct_to_pers_acc.summonses_direct_to_pers_acc_id,
				summonses_direct_to_pers_acc.count_draft_board + summonses_direct_to_pers_acc.count_recruitment_company + summonses_direct_to_pers_acc.count_military_registration + summonses_direct_to_pers_acc.count_mobilisation AS count_all,
				coalesce(round(summonses_direct_to_pers_acc.count_draft_board::numeric * 100::numeric / NULLIF((summonses_direct_to_pers_acc.count_draft_board + summonses_direct_to_pers_acc.count_recruitment_company + summonses_direct_to_pers_acc.count_military_registration + summonses_direct_to_pers_acc.count_mobilisation)::numeric,0)),0) AS percent_draft_board,
				coalesce(round(summonses_direct_to_pers_acc.count_recruitment_company::numeric * 100::numeric / NULLIF((summonses_direct_to_pers_acc.count_draft_board + summonses_direct_to_pers_acc.count_recruitment_company + summonses_direct_to_pers_acc.count_military_registration + summonses_direct_to_pers_acc.count_mobilisation)::numeric,0)),0) AS percent_recruitment_company,
				coalesce(round(summonses_direct_to_pers_acc.count_military_registration::numeric * 100::numeric / NULLIF((summonses_direct_to_pers_acc.count_draft_board + summonses_direct_to_pers_acc.count_recruitment_company + summonses_direct_to_pers_acc.count_military_registration + summonses_direct_to_pers_acc.count_mobilisation)::numeric,0)),0) AS percent_military_registration,
				coalesce(round(summonses_direct_to_pers_acc.count_mobilisation::numeric * 100::numeric / NULLIF((summonses_direct_to_pers_acc.count_draft_board + summonses_direct_to_pers_acc.count_recruitment_company + summonses_direct_to_pers_acc.count_military_registration + summonses_direct_to_pers_acc.count_mobilisation)::numeric,0)),0) AS percent_mobilisation
			   FROM summonses_list_subsystem.summonses_direct_to_pers_acc;

			ALTER TABLE summonses_list_subsystem.view_summonses_direct_to_pers_acc OWNER TO ervu_business_metrics;



			CREATE OR REPLACE VIEW summonses_list_subsystem.view_summonses_list
			AS SELECT summonses_list.summonses_list_id,
				summonses_list.count_view_arrived + summonses_list.count_not_view_arrived + summonses_list.count_view_less_20_days + summonses_list.count_not_view_less_20_days + summonses_list.count_view_greater_20_days + summonses_list.count_not_view_greater_20_days AS count_all,
				summonses_list.count_view_arrived + summonses_list.count_not_view_arrived AS count_arrived_all,
				coalesce(round((summonses_list.count_view_arrived + summonses_list.count_not_view_arrived)::numeric * 100::numeric / NULLIF((summonses_list.count_view_arrived + summonses_list.count_not_view_arrived + summonses_list.count_view_less_20_days + summonses_list.count_not_view_less_20_days + summonses_list.count_view_greater_20_days + summonses_list.count_not_view_greater_20_days)::numeric,0)),0) AS percent_arrived_all,
				coalesce(round(summonses_list.count_view_arrived::numeric * 100::numeric / NULLIF((summonses_list.count_view_arrived + summonses_list.count_not_view_arrived)::numeric,0)),0) AS percent_view_arrived,
				coalesce(round(summonses_list.count_not_view_arrived::numeric * 100::numeric / NULLIF((summonses_list.count_view_arrived + summonses_list.count_not_view_arrived)::numeric,0)),0) AS percent_not_view_arrived,
				summonses_list.count_view_less_20_days + summonses_list.count_not_view_less_20_days AS count_less_20_days_all,
				coalesce(round((summonses_list.count_view_less_20_days + summonses_list.count_not_view_less_20_days)::numeric * 100::numeric / NULLIF((summonses_list.count_view_arrived + summonses_list.count_not_view_arrived + summonses_list.count_view_less_20_days + summonses_list.count_not_view_less_20_days + summonses_list.count_view_greater_20_days + summonses_list.count_not_view_greater_20_days)::numeric,0)),0) AS percent_less_20_days_all,
				coalesce(round(summonses_list.count_view_less_20_days::numeric * 100::numeric / NULLIF((summonses_list.count_view_less_20_days + summonses_list.count_not_view_less_20_days)::numeric,0)),0) AS percent_view_less_20_days,
				coalesce(round(summonses_list.count_not_view_less_20_days::numeric * 100::numeric / NULLIF((summonses_list.count_view_less_20_days + summonses_list.count_not_view_less_20_days)::numeric,0)),0) AS percent_not_view_less_20_days,
				summonses_list.count_view_greater_20_days + summonses_list.count_not_view_greater_20_days AS count_greater_20_days_all,
				coalesce(round((summonses_list.count_view_greater_20_days + summonses_list.count_not_view_greater_20_days)::numeric * 100::numeric / NULLIF((summonses_list.count_view_arrived + summonses_list.count_not_view_arrived + summonses_list.count_view_less_20_days + summonses_list.count_not_view_less_20_days + summonses_list.count_view_greater_20_days + summonses_list.count_not_view_greater_20_days)::numeric,0)),0) AS percent_greater_20_days_all,
				coalesce(round(summonses_list.count_view_greater_20_days::numeric * 100::numeric / NULLIF((summonses_list.count_view_greater_20_days + summonses_list.count_not_view_greater_20_days)::numeric,0)),0) AS percent_view_greater_20_days,
				coalesce(round(summonses_list.count_not_view_greater_20_days::numeric * 100::numeric / NULLIF((summonses_list.count_view_greater_20_days + summonses_list.count_not_view_greater_20_days)::numeric,0)),0) AS percent_not_view_greater_20_days
			   FROM summonses_list_subsystem.summonses_list;

			ALTER TABLE summonses_list_subsystem.view_summonses_list OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
</databaseChangeLog>