
CREATE OR REPLACE VIEW apis_campaign AS (
    SELECT DISTINCT telescop as telescope, campaign, min(tdateobs) AS begin_campaign, 
        max(tdateobs) AS end_campaign 
        FROM apis_data 
        GROUP BY telescop, campaign
        ORDER BY telescop, campaign, begin_campaign, end_campaign
);


CREATE OR REPLACE VIEW apis_telescopes AS (
	SELECT DISTINCT 
		apis_data.telescop AS telescope, 
		apis_data.instrume AS instrument, 
		apis_data.detector, 
		apis_data.filter, 
		apis_data.opt_elem,
		apis_data.obstype, 
		apis_data.obsmode, 
		apis_data.photmode
	FROM apis_data
	ORDER BY apis_data.telescop, apis_data.instrume, apis_data.detector, 
			apis_data.filter, apis_data.opt_elem,  apis_data.obstype,
			apis_data.obsmode, apis_data.photmode
);



CREATE OR REPLACE VIEW apis_targets AS (
	SELECT DISTINCT apis_data.targname AS name,
		'primary' as type
		FROM apis_data
	UNION
	SELECT DISTINCT bastet_secondary_targets.targname AS name,
		'secondary' as type
		FROM bastet_secondary_targets
	ORDER BY name

);

CREATE OR REPLACE VIEW apis_secondary_targets as (
    select 'bas_' || id as data_id, targname 
    from bastet_secondary_targets
);
