SELECT 
		  a.attr_value_id, 
		  a.card_id, 
		  a.attribute_code, 
		  a.number_value, 
		  a.string_value, 
		  a.date_value,  
		  a.value_id, 
		  a.another_value, 
		  v.value_rus, 
		  v.value_eng, 
		  p.full_name, 
		  0 as ord, 
		  attr.data_type, 
		  a.long_binary_value, 
		  p.card_id 
	, (select av.string_value	 from attribute_value av 	 where av.card_id = a.card_id 	 and av.attribute_code = 	 'JBR_REGD_REGNUM' 
	) as order_0, (select date_trunc('day', av.date_value)	 from attribute_value av 	 where av.card_id = a.card_id 	 and av.attribute_code = 	 'JBR_REGD_DATEREG' 
	) as order_1
	 FROM attribute_value a  
			INNER JOIN attribute attr on attr.attribute_code=a.attribute_code 
			LEFT OUTER JOIN values_list v on a.value_id=v.value_id 
			LEFT OUTER JOIN person p on a.number_value=p.person_id 
	WHERE 
			a.attribute_code in ('JBR_IMPL_ONCONT','_STATE','ADMIN_290575','JBR_IMPL_RESOLUT','JBR_DOCL_RELATDOC','ADMIN_221237','JBR_REGD_REGNUM','JBR_REGD_DATEREG','JBR_REGD_NUMOUT','JBR_REGD_DATEOUT','JBR_INFD_SHORTDESC','JBR_INFD_SENDER','_STATE','CREATED') 
		AND a.card_id in (0) 
	
	 UNION ALL SELECT 
		   null as attr_value_id, 
		   c.card_id, 
		   '_STATE' as attribute_code, 
		   null as number_value, 
		   null as string_value, 
		   null as date_value, 
		   c.status_id as value_id, 
		   null as another_value, 
		   s.name_rus as value_rus, 
		   s.name_eng as value_eng, 
		   null as full_name, 
		   0 as ord, 
		 'L', 
		 null as long_binary_value, 
		 null as card_id 
	, null as order_0, null as order_1
	 FROM 
		   card c inner join card_status s on c.status_id=s.status_id 
	where c.card_id in (0) 
	
	 UNION SELECT 
		  null as attr_value_id, 
		  c.card_id, 
		  a.attribute_code, 
		  av.card_id as number_value, 
		  null as string_value,  
		  null as date_value, 
		  null as value_id, 
		  null as another_value, 
		  null as value_rus, 
		  null as value_eng, 
		  null as full_name, 
		  0 as ord, 
		 'B', 
		 null as long_binary_value, 
		 null as card_id 
	, null as order_0, null as order_1
	 FROM card c 
		 INNER JOIN template_block tb on tb.template_id = c.template_id 
		 INNER JOIN attribute a on a.block_code=tb.block_code and a.data_type='B' and a.attribute_code in ('JBR_IMPL_ONCONT','_STATE','ADMIN_290575','JBR_IMPL_RESOLUT','JBR_DOCL_RELATDOC','ADMIN_221237','JBR_REGD_REGNUM','JBR_REGD_DATEREG','JBR_REGD_NUMOUT','JBR_REGD_DATEOUT','JBR_INFD_SHORTDESC','JBR_INFD_SENDER','_STATE') 
		 INNER JOIN attribute_option ao on a.attribute_code = ao.attribute_code 
		 	and ao.option_code = 'LINK' 
		 INNER JOIN attribute_value av on av.number_value = c.card_id and av.attribute_code in (select * from functionsplit(ao.option_value, ';')) 
	where c.card_id in (0) 
	
	 UNION SELECT 
		  null as attr_value_id, 
		  c.card_id, 
		  a.attribute_code, 
		  av1.card_id as number_value, 
		  null as string_value,  
		  null as date_value, 
		  null as value_id, 
		  null as another_value, 
		  null as value_rus, 
		  null as value_eng, 
		  null as full_name, 
		  0 as ord, 
		 'B', 
		 null as long_binary_value, 
		 null as card_id 
	, null as order_0, null as order_1
	 FROM card c 
		 INNER JOIN template_block tb on tb.template_id = c.template_id 
		 INNER JOIN attribute a on a.block_code=tb.block_code and a.data_type='B' and a.attribute_code in ('JBR_IMPL_ONCONT','_STATE','ADMIN_290575','JBR_IMPL_RESOLUT','JBR_DOCL_RELATDOC','ADMIN_221237','JBR_REGD_REGNUM','JBR_REGD_DATEREG','JBR_REGD_NUMOUT','JBR_REGD_DATEOUT','JBR_INFD_SHORTDESC','JBR_INFD_SENDER','_STATE') 
		 INNER JOIN attribute_option ao on a.attribute_code = ao.attribute_code 
		 	and ao.option_code = 'LINK' 
		 INNER JOIN attribute_value av on av.number_value = c.card_id and av.attribute_code in (select * from functionsplit(ao.option_value, ';')) 
		 INNER JOIN attribute_option ao1 on a.attribute_code = ao1.attribute_code 
		   and ao1.option_code = 'UPLINK' 
		 INNER JOIN attribute_value av1 on av1.number_value = av.card_id and av1.attribute_code in (select * from functionsplit(ao1.option_value, ';')) 
	where c.card_id in (0) 
		 order by  order_0 asc nulls last,  order_1 asc nulls last, attr_value_id asc nulls last
