title: How to add conditions in SQL Subquery
body: <p>I have a nested sub-query to generate XML file output results.</p>
  
  <p>In the final XML results, I need to skip few elements based on conditions.</p>
  
  <p>Please help how do I go about in the below scenario?</p>
  
  <p>Query:</p>
  
  <pre><code>SELECT  InterfaceName,[Version],CreationSource,CreationTimestamp,
    (SELECT TOP 1 NULL AS BusinessUnitList,
          (SELECT  BusinessUnit.[BusinessUnit_id] AS id, 
  
           (SELECT  [User_id]AS id,[User_loginName] AS loginName ,[User_password] AS [password] ,[User_statusCode] AS statusCode,[User_firstName] AS firstName,[User_lastName] AS lastName ,[User_defaultLanguageName] AS defaultLanguageName,[User_forcePasswordChange] AS forcePasswordChange,
  
               --RoleList
                  (SELECT TOP 1 NULL AS RoleList,
                      (SELECT Role_id AS id FROM #UserRole AS [Role] WHERE [Role].User_id= [User].[User_id]   FOR XML AUTO, TYPE) 
                     FROM [dbo].[EmployeeFeedStage] AS RoleList  FOR XML AUTO, TYPE
                     ),
  
              --OrgList
                  (SELECT TOP 1 NULL AS OrgList,
                      (SELECT [Organization_id] AS id FROM [dbo].[EmployeeFeedStage] AS Organization  WHERE Organization.[User_id]= [User].[User_id] FOR XML AUTO, TYPE) 
                      FROM [dbo].[EmployeeFeedStage] AS OrgList FOR XML AUTO, TYPE
                    ),
              --Address
                  (SELECT [Address_country] AS country ,[Address_email] AS email FROM [dbo].[EmployeeFeedStage] AS [Address] WHERE [Address].[User_id]= [User].[User_id] FOR XML AUTO, TYPE) ,
  
              --Employee
                  (SELECT [Employee_badgeNumber] AS badgeNumber,[Employee_birthDate] AS birthDate,[Employee_hireDate] AS hireDate,[Employee_seniorityDate] AS seniorityDate,[Employee_managerLevelFlag] AS managerLevelFlag,[Employee_managerClockOverridePassword]  AS managerClockOverridePassword,[Employee_punchValidation] AS punchValidation,[Employee_employeeType] AS employeeType,[Employee_schedulingTypeCode] AS schedulingTypeCode, [Employee_employeeGeneratesExceptions] AS employeeGeneratesExceptions ,[Employee_employeeGeneratesAlerts] AS employeeGeneratesAlerts ,[Employee_payrollSystemNumber] AS payrollSystemNumber ,
  
  
  --AccrualConfigList
  --Here need to add condition to skip this element if AccrualConfig_id is NULL
  
                      (SELECT TOP 1 NULL AS AccrualConfigList,
                          (SELECT [AccrualConfig_id] AS id, [AccrualConfig_start] AS start FROM [dbo].[EmployeeFeedStage] AS AccrualConfig WHERE AccrualConfig.[User_id]= [User].[User_id]   FOR XML AUTO, TYPE)
                          FROM [dbo].[EmployeeFeedStage] AS AccrualConfigList  FOR XML AUTO, TYPE
                      ) ,
  
                          --Availability
  
                          (SELECT [Availability_minHoursPerWeek] AS minHoursPerWeek,[Availability_maxHoursPerWeek] AS maxHoursPerWeek,[Availability_maxDaysPerWeek]AS maxDaysPerWeek,[Availability_maxConsecutiveDays]AS maxConsecutiveDays,[Availability_maxConsecutiveDaysAcrossWeeks]AS maxConsecutiveDaysAcrossWeeks,[Availability_minHoursBetweenShifts]AS minHoursBetweenShifts,     [Availability_allowSplitShifts]AS allowSplitShifts FROM [dbo].[EmployeeFeedStage] AS [Availability] WHERE [Availability].[User_id]= [User].[User_id]  FOR XML AUTO, TYPE),
                          --Job List--
                          (SELECT TOP 1 NULL AS JobList,
                        (SELECT  [Job_id] AS id, [Job_start] AS start,   
                          (SELECT  [PrimaryJobInfo_payPolicyId] AS payPolicyId,[PrimaryJobInfo_punchRuleId] AS punchRuleId,[PrimaryJobInfo_shiftStrategyId] AS shiftStrategyId,[PrimaryJobInfo_exemptFlag] AS exemptFlag,[PrimaryJobInfo_salariedFlag] AS salariedFlag,[PrimaryJobInfo_fullTimeFlag] AS fullTimeFlag FROM [dbo].[EmployeeFeedStage] AS PrimaryJobInfo   WHERE PrimaryJobInfo.[User_id]= [User].[User_id] FOR XML AUTO, TYPE ) ,
                          (SELECT [JobRate_start] AS start,[JobRate_rate] AS rate FROM [dbo].[EmployeeFeedStage] AS JobRate WHERE JobRate.[User_id]= [User].[User_id] FOR XML AUTO, TYPE)  
  
                         FROM [dbo].[EmployeeFeedStage] AS Job WHERE Job.[User_id]= [User].[User_id] FOR XML AUTO, TYPE)
                         FROM [dbo].[EmployeeFeedStage] AS JobList WHERE JobList.[User_id]= [User].[User_id] FOR XML AUTO, TYPE),
  
                         --WorkStatusList
                         (SELECT TOP 1 NULL AS WorkStatusList,
                          (SELECT [WorkStatus_action] AS action,[WorkStatus_status] AS status,[WorkStatus_start] AS start   FROM [dbo].[EmployeeFeedStage] AS WorkStatus WHERE WorkStatus.[User_id]= [User].[User_id] FOR XML AUTO, TYPE)                    
                           FROM [dbo].[EmployeeFeedStage] AS WorkStatusList WHERE WorkStatusList.[User_id]= [User].[User_id] FOR XML AUTO, TYPE
                          )
  
                          --(SELECT TOP 1 NULL AS TimeoffAccrualList,
                          --(SELECT [TimeoffAccrual_PayAdjustmentID] AS payAdjustmentId,[TimeOffAccrual_EffectiveDate] AS effectiveDate, [TimeOffAccrual_Accrued] AS accrued FROM [dbo].[EmployeeFeedStage] AS TimeoffAccrual WHERE TimeoffAccrual.[User_id]= [User].[User_id] FOR XML AUTO, TYPE)
                          -- FROM [dbo].[EmployeeFeedStage] AS TimeoffAccrualList WHERE TimeoffAccrualList.[User_id]= [User].[User_id] FOR XML AUTO, TYPE
                          --)
  
                      FROM [dbo].[EmployeeFeedStage] AS Employee  WHERE Employee.[User_id]= [User].[User_id] FOR XML AUTO, TYPE
                  )
  
  
            FROM [dbo].[EmployeeFeedStage] AS [User] WHERE [User].[BusinessUnit_id] = BusinessUnit.[BusinessUnit_id] FOR XML AUTO, TYPE)
  
          FROM [dbo].[EmployeeFeedStage] AS BusinessUnit WHERE BusinessUnit.[BusinessUnit_id]='10027760' GROUP BY BusinessUnit.[BusinessUnit_id]  FOR XML AUTO, TYPE)  
          FROM [dbo].[EmployeeFeedStage] AS BusinessUnitList FOR XML AUTO, TYPE)
    FROM #Document AS EnterpriseDocument FOR XML AUTO, TYPE
  </code></pre>