drop view if exists UsersDataView;
create view UsersDataView as 
	select * from UsersData where 'user' in (
		select id from Users where date_out is null
	) and id in (
		select max(id) from UsersData where date_out is null group by 'user'
	);
	
drop view if exists IpsDataView;
create view IpsDataView as 
	select * from IpsData where ip in (
		select id from Ips where date_out is null
	) and id in (
		select max(id) from IpsData where date_out is null group by ip
	);
	
drop view if exists SessionsView;
create view SessionsView as 
	select 
		sessions.id as id,
		users.'user' as user_id,
		users.name as 'user',
		users.accessible as user_accesible,
		users.deleted as user_deleted,
		users.date_in as user_date_in,
		ips.ip as ip_id,
		ips.address as ip,
		ips.accessible as ip_accessible,
		ips.deleted as deleted,
		ips.date_in as ip_date_in,
		sessions.date_last as date_last,
		sessions.date_in as date_in,
		sessions.date_out as date_out
	from Sessions sessions
	join UsersDataView users on sessions.'user' = users.'user'
	join IpsDataView ips on sessions.ip = ips.ip;

drop view if exists MethodsView;
create view MethodsView as 
	select 
		methods.id as id,
		applications.id as application_id,
		applications.name as application,
		methods.name as 'method',
		applications.date_in as application_date_in,
		methods.date_in as date_in
	from Methods methods
	join Applications applications on 
		applications.date_out is null and 
		methods.application = applications.id and 
		methods.date_out is null;
	
drop view if exists LogsView;
create view LogsView as 
	select 
		logs.id as id,
		sessions.id as session_id,
		sessions.user_id as user_id,
		sessions.'user' as 'user',
		sessions.user_accesible as user_accesible,
		sessions.user_deleted as user_deleted,
		sessions.user_date_in as user_date_in,
		sessions.ip_id as ip_id,
		sessions.ip as ip,
		sessions.ip_accessible as ip_accessible,
		sessions.deleted as session_deleted,
		sessions.ip_date_in as ip_date_in,
		sessions.date_last as date_last,
		sessions.date_in as session_date_in,
		sessions.date_out as session_date_out,
		methods.id as method_id,
		methods.application_id as application_id,
		methods.application as application,
		methods.'method' as 'method',
		methods.application_date_in as application_date_in,
		methods.date_in as method_date_in,
		logs.parameters as parameters,
		logs.error as error,
		logs.date_in as date_in,
		logs.date_out as date_out
	from Logs logs
	join MethodsView methods on logs.'method' = methods.id
	left join SessionsView sessions on logs.'session' = sessions.id;
	
drop view if exists TracesView;
create view TracesView as 
	select 
		traces.id as id,
		sessions.id as session_id,
		sessions.user_id as user_id,
		sessions.'user' as 'user',
		sessions.user_accesible as user_accesible,
		sessions.user_deleted as user_deleted,
		sessions.user_date_in as user_date_in,
		sessions.ip_id as ip_id,
		sessions.ip as ip,
		sessions.ip_accessible as ip_accessible,
		sessions.deleted as session_deleted,
		sessions.ip_date_in as ip_date_in,
		sessions.date_last as date_last,
		sessions.date_in as session_date_in,
		sessions.date_out as session_date_out,
		methods.id as method_id,
		methods.application_id as application_id,
		methods.application as application,
		methods.'method' as 'method',
		methods.application_date_in as application_date_in,
		methods.date_in as method_date_in,
		traces.parameters as parameters,
		traces.error as error,
		traces.trace as trace,
		traces.message as message,
		traces.date_in as date_in,
		traces.date_out as date_out
	from Traces traces
	join MethodsView methods on traces.'method' = methods.id
	left join SessionsView sessions on traces.'session' = sessions.id;