2017-02-27 16 views
0

XMLファイルの出力結果を生成するネストされたサブクエリがあります。SQLサブクエリで条件を追加する方法

最終的なXML結果では、条件に基づいていくつかの要素をスキップする必要があります。

以下のシナリオではどうすればよいですか?

問合せ:

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 
+0

にスキップされ--AccrualConfigListを参照してください。ここに条件が必要です – LakshmiNagani

+1

コードを必要最小限に抑えてください。繰り返される(しかし無関係な)部分は削除する必要があります。あなたが得た(減らされた)出力とあなたの期待される出力がどのように見えるかをposteします。 [良いSQL質問する方法](http://meta.stackoverflow.com/questions/271055/tips-for-asking-a-good-structured-query-language-sql-question/271056)と[ MCVEを作成する方法](http://stackoverflow.com/help/mcve) – Shnugo

答えて

0

クイックデモ、null値がfor xml auto, type

with t as (
    select 1 c1, 2 c2 
    union all 
    select 3, 4 
    union all 
    select 5, 6 
) 
select c1, (
     select c1, case when c2 < 6 then c2 end as c2 
     from t t2 
     where t1.c1=t2.c1 
     for xml auto, type) xml 
from t t1 ; 
+0

現在の実装 – LakshmiNagani

+0

(([Employee_badgeNumber] badgeNumber AS、[Employee_birthDate]たbirthDate AS、[Employee_hireDate] HIREDATE、AS --AccrualConfigList を選択[AccualConfig_id] AS IDから[AccrualConfig_start] AS開始FROM [dbo]。[EmployeeFeedStage] AS AccrualConfig for XML AUTO、TYPE) FROM [dbo]。[EmployeeFeedStage] AS AccrualConfigList FOR XML AUTO、TYPE )、 - 仕事リスト - - 仕事リストのサブクエリ [DBO] FROM [EmployeeFeedStage]社員[USER_ID] =は、[ユーザー] [USER_ID] XML AUTO、TYPE – LakshmiNagani

+0

FOR(誕生​​日、 ようEmployee_birthDate]、badgeNumberようEmployee_badgeNumber]を選択従業員として。。。 - AccrualConfig_id]がNULLの場合、SkipAccrual の場合(SELECT TOP 1 NULL AS AccrualConfigList、 (SELECT [AccrualConfig_id] AS id、[AccrualConfig_start] AS開始FROM [dbo]。[EmployeeFeedStage] As AccrualConfig for XML AUTO、TYPE) [DBO] FROM) [EmployeeFeedStage] XML AUTO、TYPE )FOR AccrualConfigList、 SkipAccrual AS:。 --Job List-- --SubQueryジョブリストについてFROM [dbo]。[EmployeeFeedStage]従業員の従業員。[User_id] = [User]。[User_id] FOR XML AUTO、TYPE – LakshmiNagani

関連する問題