create view view_web_cluster_edit_subcl
as
(
   select c2c5.cluster_id , c2c5.cluster_recursion_id, ch5.state_change, ch5.name, ch5.alias, ch5.state, ch5.clusterlogic_id, ch5.clustertype_id  from        
   (   
       (  

	SELECT DISTINCT c2c3.cluster_id, c2c3.cluster_recursion_id from
	(
	  select c2c1.cluster_id, max(c2ch1.history_timestamp) as history_timestamp, c2c1.cluster_recursion_id 
          from cluster2component c2c1, cluster2component_history c2ch1 
	  where c2c1.c2c_id = c2ch1.c2c_id and c2c1.cluster_recursion_id  IS NOT NULL
	  and c2ch1.history_timestamp < CURRENT_TIMESTAMP
	  group by  c2c1.cluster_id, c2c1.cluster_recursion_id 
	) as c2c3


	join

	(
	  select c2c2.cluster_id, c2ch2.history_timestamp, c2c2.cluster_recursion_id 
          from cluster2component c2c2, cluster2component_history c2ch2 
	  where c2c2.c2c_id = c2ch2.c2c_id and c2c2.cluster_recursion_id  IS NOT NULL
	  and c2ch2.valid = TRUE
	) as c2c4

        on ( c2c3.cluster_id = c2c4.cluster_id and c2c3.history_timestamp = c2c4.history_timestamp  and c2c3.cluster_recursion_id = c2c4.cluster_recursion_id )

       )


       UNION       
      (
 	      

    	 select DISTINCT c2c10.cluster_id, c2c10.cluster_recursion_id 
    	 from cluster2component c2c10, cluster2component_history c2ch10 
    	 where c2c10.c2c_id = c2ch10.c2c_id and c2c10.cluster_recursion_id  IS NOT NULL
    	 and c2ch10.history_timestamp >= CURRENT_TIMESTAMP
      )
      
   ) as c2c5


   join	   

   (


     select ch4.cluster_id, ch4.state_change, ch4.name, ch4.alias, ch4.state, ch4.clusterlogic_id, ch4.clustertype_id from
     (
       select ch1.cluster_id,  max(ch1.state_change) as state_change
       from cluster_history ch1
       group by  ch1.cluster_id
     ) as ch3

     join

     (
       select ch2.cluster_id, ch2.state_change, ch2.name, ch2.alias, ch2.state, ch2.clusterlogic_id, ch2.clustertype_id
       from cluster_history ch2
     ) as ch4

     on ( ch3.cluster_id = ch4.cluster_id and ch3.state_change = ch4.state_change)
     order by ch4.cluster_id
   ) as ch5	

   on (c2c5.cluster_recursion_id= ch5.cluster_id) 
);

-- This create view statement works


-- Here is the output of the view from postgresql (debian woody, postgres 7.2.1) 
-- output of \dv view_web_cluster_edit_subcl
-- View definition: 


SELECT c2c5.cluster_id, c2c5.cluster_recursion_id, ch5.state_change, ch5.name, ch5.alias, ch5.state, ch5.clusterlogic_id, ch5.clustertype_id 
FROM ((SELECT DISTINCT c2c3.cluster_id, c2c3.cluster_recursion_id 
FROM ((SELECT c2c1.cluster_id, max(c2ch1.history_timestamp) AS history_timestamp, c2c1.cluster_recursion_id 
FROM cluster2component c2c1, cluster2component_history c2ch1 WHERE (((c2c1.c2c_id = c2ch1.c2c_id) AND (c2c1.cluster_recursion_id IS NOT NULL)) 
AND (timestamptz(c2ch1.history_timestamp) < ('now'::text)::timestamp(6) with time zone)) GROUP BY c2c1.cluster_id, c2c1.cluster_recursion_id) c2c3 
JOIN (SELECT c2c2.cluster_id, c2ch2.history_timestamp, c2c2.cluster_recursion_id FROM cluster2component c2c2, cluster2component_history c2ch2 
WHERE (((c2c2.c2c_id = c2ch2.c2c_id) AND (c2c2.cluster_recursion_id IS NOT NULL)) AND (c2ch2."valid" = 't'::bool))) c2c4 
ON ((((c2c3.cluster_id = c2c4.cluster_id) AND (c2c3.history_timestamp = c2c4.history_timestamp)) 
AND (c2c3.cluster_recursion_id = c2c4.cluster_recursion_id)))) ORDER BY c2c3.cluster_id, c2c3.cluster_recursion_id 
UNION 
SELECT DISTINCT c2c10.cluster_id, c2c10.cluster_recursion_id FROM cluster2component c2c10, cluster2component_history c2ch10 
WHERE (((c2c10.c2c_id = c2ch10.c2c_id) AND (c2c10.cluster_recursion_id IS NOT NULL)) 
AND (timestamptz(c2ch10.history_timestamp) >= ('now'::text)::timestamp(6) with time zone)) 
ORDER BY c2c10.cluster_id, c2c10.cluster_recursion_id) c2c5 
JOIN (SELECT ch4.cluster_id, ch4.state_change, ch4.name, ch4.alias, ch4.state, ch4.clusterlogic_id, ch4.clustertype_id 
FROM ((SELECT ch1.cluster_id, max(ch1.state_change) AS state_change FROM cluster_history ch1 GROUP BY ch1.cluster_id) ch3 
JOIN (SELECT ch2.cluster_id, ch2.state_change, ch2.name, ch2.alias, ch2.state, ch2.clusterlogic_id, ch2.clustertype_id F
ROM cluster_history ch2) ch4 ON (((ch3.cluster_id = ch4.cluster_id) AND (ch3.state_change = ch4.state_change)))) 
ORDER BY ch4.cluster_id) ch5 ON ((c2c5.cluster_recursion_id = ch5.cluster_id)));

--Errormessage if i put this view definiotion into psql
ERROR:  parser: parse error at or near "SELECT"

