DROP VIEW eht_view;

CREATE OR REPLACE VIEW eht_view AS
SELECT
	e.*,
	h.host,
	t.tag,
	(select string_agg(tag, ', ')
	from tags t2, episodes_tags_xref et2
	where et2.tags_id = t2.id
	group by et2.episodes_id
	having et2.episodes_id = e.id) as tags
FROM episodes e, hosts h, episodes_hosts_xref eh, episodes_tags_xref et, tags t
where e.id = eh.episodes_id
and h.id = eh.hosts_id
and e.id = et.episodes_id
and et.tags_id = t.id
group by e.id,h.host,t.tag
order by e.id;

ALTER TABLE eht_view
  OWNER TO hpradmin;
