<?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>ALTER TABLE</comment>
		<sql>
			ALTER TABLE IF EXISTS admin_indicators.user_analysis
				ADD COLUMN IF NOT EXISTS count_responsible_zi bigint NOT NULL DEFAULT 0;
			COMMENT ON COLUMN admin_indicators.user_analysis.count_responsible_zi
				IS 'Ответственный за ЗИ';


			ALTER TABLE IF EXISTS admin_indicators.user_analysis
				ADD COLUMN IF NOT EXISTS count_responsible_zi_svk bigint NOT NULL DEFAULT 0;
			COMMENT ON COLUMN admin_indicators.user_analysis.count_responsible_zi_svk
				IS 'Ответственный за ЗИ СВК';
		</sql>
	</changeSet>
	
	<changeSet id="0002" author="saliakhov">
        <comment>CREATE VIEW</comment>
		<sql>
			DROP VIEW admin_indicators.view_user_analysis;

			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_responsible_zi_svk + user_analysis.count_responsible_zi + 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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) 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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) 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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) 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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) 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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) 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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) 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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) 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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) 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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) AS percent_observer_vk,
				COALESCE(round(user_analysis.count_responsible_zi::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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) AS percent_responsible_zi,
				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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) 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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) AS percent_specialist_acquisition,
				COALESCE(round(user_analysis.count_responsible_zi_svk::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_responsible_zi_svk + user_analysis.count_responsible_zi + user_analysis.count_specialist_military_accounting + user_analysis.count_specialist_acquisition)::numeric, 0::numeric)), 0::numeric) AS percent_responsible_zi_svk   
			   FROM admin_indicators.user_analysis;

			ALTER TABLE admin_indicators.view_user_analysis
				OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
</databaseChangeLog>