
ALTER VIEW [dbo].[V_ENTITY_ATTRIBUTES]
AS
SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+900+t1.SectionLevel AS [Index],
    1 AS [AttributeType],
    t1.SectionAlias AS [Name],
    t1.Name AS  [Description],
    (case when t3.EntName is not null then t3.[Schema]+'.'+t3.EntName else t4.[Schema]+'.'+t4.EntName end) AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CASE WHEN t2.EntityID = 541 THEN CAST(1 as bit) ELSE CAST(0 as bit) END AS   [IsRequired],
    1 AS [MappingType],
    t1.SectionAlias AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CAST(NULL  AS varchar(max)) AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]

    FROM [FIES].T_PLAN_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntityID in (541, 546, 549, 563, 535, 536, 519, 571, 573, 575)
    LEFT JOIN [VCLib].T_ENTITIES t3 on t3.EntityID = t1.Entities
    LEFT JOIN [VCLib].T_ADDITIONAL_ENTITIES t4 on t4.EntityID = t1.Entities
    
    UNION ALL
    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+900+t1.[Priority] AS [Index],
    0 AS [AttributeType],
    t1.PeriodAlias AS [Name],
    t1.Name AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(1 as bit) AS   [IsRequired],
    1 AS [MappingType],
    t1.PeriodAlias AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CAST(NULL  AS varchar(max)) AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [FIES].T_PERIOD_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntityID= 600
    where t1.PeriodSection is null

    UNION ALL

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+920+t1.ParametrOrder AS [Index],
    1 AS [AttributeType],
    t1.[ParameterAlias] AS [Name],
    t1.Name AS  [Description],
    'FIES.PLAN_VALUE' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL as varchar(3)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CAST('<lx:ChildCount xmlns:lx="lexem">
    <condition>
        <lx:Criterion type="=">
            <lx:Path OwnerQueryLevel="0">
                <lx:StepInt index="5440002" />
                <lx:StepInt index="5420001" />
            </lx:Path>
            <lx:ConstOperand type="Guid">'+ cast(t1.PayParametrID as varchar(40))+'</lx:ConstOperand>
        </lx:Criterion>
    </condition>
    <lx:Path OwnerQueryLevel="0">
        <lx:StepInt index="5410003" />
    </lx:Path>
    <lx:Maximum>
        <lx:Path OwnerQueryLevel="0">
        <lx:StepCurrent />
    </lx:Path>
    </lx:Maximum>
</lx:ChildCount>'  AS varchar(max)) AS   [AttrLexem],

    '{5410003}' AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]

    FROM [FIES].[T_PLAN_PARAMETER] t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntityID= 541
    

    UNION ALL

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+1000+(t3.DisplayOrder*10+t1.ParametrOrder) AS [Index],
    1 AS [AttributeType],
    t3.TypeAlias +  t1.GroupAlias AS [Name],
    t1.Name + ' (' + t3.Name + ')' AS  [Description],
    'FIES.LIMIT_VALUE' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL as varchar(3)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CAST('<lx:QueryValue entity="FIES.LIMIT_VALUE" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5470002" />
                    <lx:StepInt index="5460001" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5460001" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5470006" />
                    <lx:StepInt index="5520001" />
                </lx:Path>
                <lx:ConstOperand type="Guid">'+ cast(t1.ParameterGroupID as varchar(36)) +'</lx:ConstOperand>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5470005" />
                    <lx:StepInt index="5510001" />
                </lx:Path>
                <lx:ConstOperand type="Guid">'+ cast(t3.PlanLimitTypeID as varchar(36)) +'</lx:ConstOperand>
            </lx:Criterion>
        </lx:Criteria>
    </condition>
    <value>
        <lx:Path OwnerQueryLevel="0">
            <lx:StepInt index="5470001" />
        </lx:Path>
    </value>
</lx:QueryValue>'  AS varchar(max)) AS   [AttrLexem],
    CAST(NULL AS VARCHAR(MAX)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    from [FIES].T_PARAMETER_GROUP t1, [VCLib].T_ENTITIES t2, [FIES].T_PLAN_LIMIT_TYPE t3
    where t2.EntityID = 546

    UNION ALL

    -- ССЫЛКА НА ДЕЙСТВУЮЩУЮ ЗАПИСЬ ЗАЯВКИ

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+888 AS [Index],
    1 AS [AttributeType],
    'ActualPlanItem' AS [Name],
    'Действующая запись плана' AS  [Description],
    t2.[Schema]+'.'+t2.EntName AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.PLAN_ITEM" top="1"  xmlns:lx="lexem">
  <condition>
    <lx:Criteria type="And">
      <lx:Criterion type="=">
        <lx:Path OwnerQueryLevel="0">
          <lx:StepInt index="5410002" />
          <lx:StepInt index="5390006" />
        </lx:Path>
        <lx:Path OwnerQueryLevel="1">
          <lx:StepInt index="5410002" />
          <lx:StepInt index="5390006" />
        </lx:Path>
      </lx:Criterion>
      <lx:Criterion type="=">
        <lx:Path OwnerQueryLevel="0">
          <lx:StepInt index="5410002" />
          <lx:StepInt index="5390004" />
        </lx:Path>
        <lx:Path OwnerQueryLevel="1">
          <lx:StepInt index="5410002" />
          <lx:StepInt index="5390004" />
        </lx:Path>
      </lx:Criterion>' +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                  <lx:StepInt index="'+CAST(t2.EntityID*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                  <lx:StepInt index="'+CAST(t2.EntityID*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
              </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
            JOIN [VCLib].T_ENTITIES t2 on t2.EntityID= 541
            LEFT JOIN [VCLib].T_ENTITIES t3 on t3.EntityID = t1.Entities
            LEFT JOIN [VCLib].T_ADDITIONAL_ENTITIES t4 on t4.EntityID = t1.Entities
    )
    + '<lx:Criterion type="=">
        <lx:Path OwnerQueryLevel="0">
          <lx:StepInt index="5410002" />
          <lx:StepAs as="FIES.PLAN_REQUEST" />
          <lx:StepInt index="5400002" />
        </lx:Path>
        <lx:ChoiceOperand Entity="FIES.PLAN_REQUEST" AttributeIndex="5400002" PrimaryKey="5" />
      </lx:Criterion>
      <lx:Criterion type="=">
        <lx:Path OwnerQueryLevel="0">
          <lx:StepInt index="5410002" />
          <lx:StepAs as="FIES.PLAN_REQUEST" />
          <lx:StepInt index="5400007" />
        </lx:Path>
        <lx:ChoiceOperand Entity="FIES.PLAN_REQUEST" AttributeIndex="5400007" PrimaryKey="5" />
      </lx:Criterion>
    </lx:Criteria>
  </condition>
  <orderBy>
    <lx:Path OwnerQueryLevel="0">
      <lx:StepInt index="5410002" />
      <lx:StepAs as="FIES.PLAN_REQUEST" />
      <lx:StepInt index="5400006" />
    </lx:Path>
    <desc>True</desc>
    <lx:Path OwnerQueryLevel="0">
		<lx:StepInt index="5410001" />
	</lx:Path>
	<desc>False</desc>
  </orderBy>
  <value>
    <lx:Path OwnerQueryLevel="0">
      <lx:StepInt index="5410001" />
    </lx:Path>
  </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [VCLib].T_ENTITIES t2 
    where EntityID = 541

    UNION ALL

    -- ССЫЛКА НА ПРЕДЫДУЩУЮ ЗАПИСЬ ЗАЯВКИ

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+889 AS [Index],
    1 AS [AttributeType],
    'PrevPlanItem' AS [Name],
    'Предыдущая запись плана' AS  [Description],
    t2.[Schema]+'.'+t2.EntName AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.PLAN_ITEM" top="1"  xmlns:lx="lexem">
  <condition>
    <lx:Criteria type="And">
      <lx:Criterion type="&lt;=">
        <lx:Path OwnerQueryLevel="0">
         <lx:StepInt index="5410002" />
         <lx:StepInt index="5390006" />
         <lx:StepInt index="5500002" />
        </lx:Path>
        <lx:Path OwnerQueryLevel="1">
          <lx:StepInt index="5410002" />
          <lx:StepInt index="5390006" />
          <lx:StepInt index="5500002" />
        </lx:Path>
      </lx:Criterion>
      <lx:Criterion type="=">
        <lx:Path OwnerQueryLevel="0">
          <lx:StepInt index="5410002" />
          <lx:StepInt index="5390004" />
        </lx:Path>
        <lx:Path OwnerQueryLevel="1">
          <lx:StepInt index="5410002" />
          <lx:StepInt index="5390004" />
        </lx:Path>
      </lx:Criterion>' +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                  <lx:StepInt index="'+CAST(t2.EntityID*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                  <lx:StepInt index="'+CAST(t2.EntityID*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
              </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
            JOIN [VCLib].T_ENTITIES t2 on t2.EntityID= 541
            LEFT JOIN [VCLib].T_ENTITIES t3 on t3.EntityID = t1.Entities
            LEFT JOIN [VCLib].T_ADDITIONAL_ENTITIES t4 on t4.EntityID = t1.Entities
    )
    + '<lx:Criterion type="=">
        <lx:Path OwnerQueryLevel="0">
          <lx:StepInt index="5410002" />
          <lx:StepAs as="FIES.PLAN_REQUEST" />
          <lx:StepInt index="5400002" />
        </lx:Path>
        <lx:ChoiceOperand Entity="FIES.PLAN_REQUEST" AttributeIndex="5400002" PrimaryKey="5" />
      </lx:Criterion>
      <lx:Criterion type="=">
        <lx:Path OwnerQueryLevel="0">
          <lx:StepInt index="5410002" />
          <lx:StepAs as="FIES.PLAN_REQUEST" />
          <lx:StepInt index="5400007" />
        </lx:Path>
        <lx:ChoiceOperand Entity="FIES.PLAN_REQUEST" AttributeIndex="5400007" PrimaryKey="5" />
      </lx:Criterion>
      <lx:Criterion type="&lt;">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5410002" />
                    <lx:StepAs as="FIES.PLAN_REQUEST" />
                    <lx:StepInt index="5400006" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5410002" />
                    <lx:StepAs as="FIES.PLAN_REQUEST" />
                    <lx:StepInt index="5400006" />
                </lx:Path>
        </lx:Criterion>
    </lx:Criteria>
  </condition>
  <orderBy>
    <lx:Path OwnerQueryLevel="0">
      <lx:StepInt index="5410002" />
      <lx:StepAs as="FIES.PLAN_REQUEST" />
      <lx:StepInt index="5400006" />
    </lx:Path>
    <desc>True</desc>
    <lx:Path OwnerQueryLevel="0">
		<lx:StepInt index="5410001" />
	</lx:Path>
	<desc>False</desc>
  </orderBy>
  <value>
    <lx:Path OwnerQueryLevel="0">
      <lx:StepInt index="5410001" />
    </lx:Path>
  </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [VCLib].T_ENTITIES t2 
    where EntityID = 541

	-- ССЫЛКА НА ПОДПРОГРАММУ
	UNION ALL

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+900 AS [Index],
    1 AS [AttributeType],
    'Subprogramm' AS [Name],
    'Подпрограмма' AS  [Description],
    'FIES.SUBPROGRAMM' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CASE WHEN (SELECT COUNT(*) FROM [FIES].T_SUBPROGRAMM)= 0 THEN '<lx:ConstOperand isNull="1" xmlns:lx="lexem" />'
	     ELSE '<lx:QueryValue entity="FIES.SUBPROGRAMM" top="1"  xmlns:lx="lexem">
		<condition>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5720001" />
				</lx:Path>
				<lx:Case>' +
				 (
					CAST((SELECT  CAST('<When xmlns:lx="lexem">
						'+ REPLACE([dbo].[RegExReplace](cast(t1.Condition as varchar(max)), '<lx:BaseObjectOperand Entity="[\w\.]+" PrimaryKey="(?<guid>[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"\s*\/>', '<lx:ConstOperand type="Guid">${guid}</lx:ConstOperand>'), '<lx:Path OwnerQueryLevel="0">', '<lx:Path OwnerQueryLevel="1">')
						 +'
					</When>
					<Then xmlns:lx="lexem">
						<lx:ConstOperand type="Guid">' + CAST(t1.SubprogrammID as varchar(36)) + '</lx:ConstOperand>
					</Then>' AS xml)
					FROM [FIES].T_SUBPROGRAMM t1
					ORDER BY t1.[Priority]
					FOR XML PATH(''), TYPE	) AS VARCHAR(MAX))	
					
				 )
				 + '			<Else>
						<lx:ConstOperand isNull="1" />
					</Else>
				</lx:Case>
			</lx:Criterion>
		</condition>
		<value>
			<lx:Path OwnerQueryLevel="0">
				<lx:StepInt index="5720001" />
			</lx:Path>
		</value>
	</lx:QueryValue>'
		 END AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [VCLib].T_ENTITIES t2 
    where EntityID = 544
	--[dbo].[RegExReplace](@test, '<lx:BaseObjectOperand Entity="[\w\.]+" PrimaryKey="(?<guid>[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"\s*\/>', '<lx:ConstOperand type="Guid">${guid}</lx:ConstOperand>')
   

   -- ССЫЛКА НА ПОДПРОГРАММУ ID
	UNION ALL

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+901 AS [Index],
    0 AS [AttributeType],
    'SubprogrammID' AS [Name],
    'Подпрограмма(ID)' AS  [Description],
    'guid' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CASE WHEN (SELECT COUNT(*) FROM [FIES].T_SUBPROGRAMM)= 0 THEN '<lx:ConstOperand isNull="1" xmlns:lx="lexem" />'
	     ELSE '<lx:Case  xmlns:lx="lexem">' +
				 (
					CAST((SELECT  CAST('<When xmlns:lx="lexem">
						'+ [dbo].[RegExReplace](cast(t1.Condition as varchar(max)), '<lx:BaseObjectOperand Entity="[\w\.]+" PrimaryKey="(?<guid>[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"\s*\/>', '<lx:ConstOperand type="Guid">${guid}</lx:ConstOperand>')
						 +'
					</When>
					<Then xmlns:lx="lexem">
						<lx:ConstOperand type="Guid">' + CAST(t1.SubprogrammID as varchar(36)) + '</lx:ConstOperand>
					</Then>' AS xml)
					FROM [FIES].T_SUBPROGRAMM t1
					ORDER BY t1.[Priority]
					FOR XML PATH(''), TYPE	) AS VARCHAR(MAX))	
					
				 )
				 + '			<Else>
						<lx:ConstOperand isNull="1" />
					</Else>
				</lx:Case>'
		 END AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [VCLib].T_ENTITIES t2 
    where EntityID = 544
	--[dbo].[RegExReplace](@test, '<lx:BaseObjectOperand Entity="[\w\.]+" PrimaryKey="(?<guid>[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"\s*\/>', '<lx:ConstOperand type="Guid">${guid}</lx:ConstOperand>')
   

   UNION ALL

-- Фактические показатели по месяцам

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+950+t1.[Priority] AS [Index],
    0 AS [AttributeType],
    t1.PeriodAlias + '_FACT' AS [Name],
    t1.Name + ' (Факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
	<condition>
		<lx:Criteria type="And">
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360007" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5450001" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390006" />
					<lx:StepInt index="5500002" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360003" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5450001" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390004" />
				</lx:Path>
			</lx:Criterion>'
			+
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5450001" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
			</lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )

	
	+'		<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360004" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5450001" />
					<lx:StepInt index="5440002" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360005" />
					<lx:StepInt index="5600001" />
				</lx:Path>
				<lx:ConstOperand type="Guid">'+ CAST(t1.PeriodSectionID AS VARCHAR(36)) +'</lx:ConstOperand>
			</lx:Criterion>
		</lx:Criteria>
	</condition>
	<value>
		<lx:IsNull>
			<lx:Sum>
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360006" />
				</lx:Path>
			</lx:Sum>
			<lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
		</lx:IsNull>
	</value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [FIES].T_PERIOD_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntityID= 545
    where t1.PeriodSection is null



UNION ALL
-- Фактические показатели по месяцам ПО СМО

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+950+t1.[Priority] AS [Index],
    0 AS [AttributeType],
    t1.PeriodAlias + '_FACT' AS [Name],
    t1.Name + ' (Факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
	<condition>
		<lx:Criteria type="And">
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360007" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390006" />
					<lx:StepInt index="5500002" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360003" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390004" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360002" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550006" />
				</lx:Path>
			</lx:Criterion>
			'
			+
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
			</lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )
	
	+'
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360901" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410901" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360902" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410902" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360903" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410903" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360904" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410904" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360004" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440002" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360005" />
					<lx:StepInt index="5600001" />
				</lx:Path>
				<lx:ConstOperand type="Guid">'+ CAST(t1.PeriodSectionID AS VARCHAR(36)) +'</lx:ConstOperand>
			</lx:Criterion>
		</lx:Criteria>
	</condition>
	<value>
		<lx:IsNull>
			<lx:Sum>
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360006" />
				</lx:Path>
			</lx:Sum>
			<lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
		</lx:IsNull>
	</value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [FIES].T_PERIOD_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntityID= 556
    where t1.PeriodSection is null

	UNION ALL

-- Сумма фактических показателей по году

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+890 AS [Index],
    0 AS [AttributeType],
    'ValueFact' AS [Name],
    'Значение (факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    '{0:0.######}' AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
	<condition>
		<lx:Criteria type="And">
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360007" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390006" />
					<lx:StepInt index="5500002" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360003" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390004" />
				</lx:Path>
			</lx:Criterion>
			'
			+
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5440003" />
					<lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
			</lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )

	
	+'
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360004" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5440002" />
				</lx:Path>
			</lx:Criterion>
		</lx:Criteria>
	</condition>
	<value>
		<lx:IsNull>
			<lx:Sum>
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360006" />
				</lx:Path>
			</lx:Sum>
			<lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
		</lx:IsNull>
	</value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [VCLib].T_ENTITIES t2
    where t2.EntityID= 544


 UNION ALL

-- Фактические показатели по месяцам СУММЫ

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+955+t1.[Priority] AS [Index],
    0 AS [AttributeType],
    t1.PeriodAlias + '_FACT_SUM' AS [Name],
    t1.Name + ' (Сумма факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
	<condition>
		<lx:Criteria type="And">
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360007" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5450001" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390006" />
					<lx:StepInt index="5500002" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360003" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5450001" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390004" />
				</lx:Path>
			</lx:Criterion>'
			+
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5450001" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
			</lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )

	
	+'		<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360004" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5450001" />
					<lx:StepInt index="5440002" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360005" />
					<lx:StepInt index="5600001" />
				</lx:Path>
				<lx:ConstOperand type="Guid">'+ CAST(t1.PeriodSectionID AS VARCHAR(36)) +'</lx:ConstOperand>
			</lx:Criterion>
		</lx:Criteria>
	</condition>
	<value>
		<lx:IsNull>
			<lx:Sum>
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360008" />
				</lx:Path>
			</lx:Sum>
			<lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
		</lx:IsNull>
	</value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [FIES].T_PERIOD_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntityID= 545
    where t1.PeriodSection is null



UNION ALL
-- Фактические показатели по месяцам ПО СМО СУММЫ

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+955+t1.[Priority] AS [Index],
    0 AS [AttributeType],
    t1.PeriodAlias + '_FACT_SUM' AS [Name],
    t1.Name + ' (Сумма факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
	<condition>
		<lx:Criteria type="And">
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360007" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390006" />
					<lx:StepInt index="5500002" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360003" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390004" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360002" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550006" />
				</lx:Path>
			</lx:Criterion>
			'
			+
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
			</lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )
	
	+'
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360901" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410901" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360902" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410902" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360903" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410903" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360904" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410904" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360004" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5560001" />
					<lx:StepInt index="5550007" />
					<lx:StepInt index="5440002" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360005" />
					<lx:StepInt index="5600001" />
				</lx:Path>
				<lx:ConstOperand type="Guid">'+ CAST(t1.PeriodSectionID AS VARCHAR(36)) +'</lx:ConstOperand>
			</lx:Criterion>
		</lx:Criteria>
	</condition>
	<value>
		<lx:IsNull>
			<lx:Sum>
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360008" />
				</lx:Path>
			</lx:Sum>
			<lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
		</lx:IsNull>
	</value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [FIES].T_PERIOD_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntityID= 556
    where t1.PeriodSection is null

	UNION ALL

-- Сумма фактических показателей по году СУММЫ

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+891 AS [Index],
    0 AS [AttributeType],
    'ValueFactSum' AS [Name],
    'Значение (Сумма факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    '{0:0.######}' AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
	<condition>
		<lx:Criteria type="And">
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360007" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390006" />
					<lx:StepInt index="5500002" />
				</lx:Path>
			</lx:Criterion>
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360003" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5440003" />
					<lx:StepInt index="5410002" />
					<lx:StepInt index="5390004" />
				</lx:Path>
			</lx:Criterion>
			'
			+
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5440003" />
					<lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
				</lx:Path>
			</lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )

	
	+'
			<lx:Criterion type="=">
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360004" />
				</lx:Path>
				<lx:Path OwnerQueryLevel="1">
					<lx:StepInt index="5440002" />
				</lx:Path>
			</lx:Criterion>
		</lx:Criteria>
	</condition>
	<value>
		<lx:IsNull>
			<lx:Sum>
				<lx:Path OwnerQueryLevel="0">
					<lx:StepInt index="5360008" />
				</lx:Path>
			</lx:Sum>
			<lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
		</lx:IsNull>
	</value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed]
    FROM [VCLib].T_ENTITIES t2
    where t2.EntityID= 544


GO


