嵌套行的 SQL FOR XML 列表

SQL FOR XML list of nested rows(嵌套行的 SQL FOR XML 列表)
本文介绍了嵌套行的 SQL FOR XML 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有这个超长的 SQL 查询,现在这个查询执行以下操作,创建第一个级别,即 Job_No....第二个级别中的下一个获取所有BaselineStart",但是这将每个 BaselineStart 与 baseOrSchedStartList 分开,我想要做的是为每个 Job_No 设置 1 个 baseOrSchedStartList,并在 baseOrSchedStartList 下设置每个字符串".我希望这是有道理的.这是我的查询:

I have this super long SQL query, now what this query does is the following, creates the first level which is the Job_No....next in the second level gets all the 'BaselineStart' however this separates each BaselineStart with baseOrSchedStartList, what I am looking to do is have 1 baseOrSchedStartList for each Job_No and have each 'string' under baseOrSchedStartList. I hope this makes sense. Here is my query:

SELECT [Job_No] as '@Key',
(
        SELECT ISNULL(UserDate1,ScheduleTasks.BaselineStart) AS 'string' 

        FROM ScheduleTasks INNER JOIN Schedule ON ScheduleTasks.ScheduleID = Schedule.ScheduleID INNER JOIN V_CONSTAT_ACTUAL_DATES ON V_CONSTAT_ACTUAL_DATES.JOB_NUMBER = Schedule.Job_No WHERE Job_No IN (SELECT JOB_NUMBER
                        FROM V_CONSTAT_ACTUAL_DATES
                        WHERE AREA_DESC = 'Orchard Park'
                        AND (DATE_TO_END IS NOT NULL AND DATE_TO_END >= GETDATE())) AND
               (
                        LibraryTaskID <> 203
                        AND LibraryTaskID <> 204
                        AND LibraryTaskID <> 210
                        AND LibraryTaskID <> 211
                        AND LibraryTaskID <> 214
                        AND LibraryTaskID <> 215
                        AND LibraryTaskID <> 218
                        AND LibraryTaskID <> 219
                        AND LibraryTaskID <> 224
                        AND LibraryTaskID <> 227
                        AND LibraryTaskID <> 230
                        AND LibraryTaskID <> 231
                        AND LibraryTaskID <> 232
                        AND LibraryTaskID <> 233
                        AND LibraryTaskID <> 234
                        AND LibraryTaskID <> 235
                        AND LibraryTaskID <> 236
                        AND LibraryTaskID <> 237        
                        AND LibraryTaskID <> 238
                        AND LibraryTaskID <> 239
                        AND LibraryTaskID <> 240
                        AND LibraryTaskID <> 242
                        AND LibraryTaskID <> 243
                        AND LibraryTaskID <> 295
                        AND LibraryTaskID <> 299
                        AND LibraryTaskID <> 303
                        AND LibraryTaskID <> 304
                        AND LibraryTaskID <> 305
                        AND LibraryTaskID <> 313
                        AND LibraryTaskID <> 314
                        AND LibraryTaskID <> 321
                        AND LibraryTaskID <> 333

               )

            ORDER BY DATE_TO_END, SortOrder
            FOR XML PATH('baseOrSchedStartList'), Type
)
FROM ScheduleTasks INNER JOIN Schedule ON ScheduleTasks.ScheduleID = Schedule.ScheduleID INNER JOIN V_CONSTAT_ACTUAL_DATES ON V_CONSTAT_ACTUAL_DATES.JOB_NUMBER = Schedule.Job_No WHERE Job_No IN (SELECT JOB_NUMBER
                        FROM V_CONSTAT_ACTUAL_DATES
                        WHERE AREA_DESC = 'Orchard Park'
                        AND (DATE_TO_END IS NOT NULL AND DATE_TO_END >= GETDATE())) AND
               (
                        LibraryTaskID <> 203
                        AND LibraryTaskID <> 204
                        AND LibraryTaskID <> 210
                        AND LibraryTaskID <> 211
                        AND LibraryTaskID <> 214
                        AND LibraryTaskID <> 215
                        AND LibraryTaskID <> 218
                        AND LibraryTaskID <> 219
                        AND LibraryTaskID <> 224
                        AND LibraryTaskID <> 227
                        AND LibraryTaskID <> 230
                        AND LibraryTaskID <> 231
                        AND LibraryTaskID <> 232
                        AND LibraryTaskID <> 233
                        AND LibraryTaskID <> 234
                        AND LibraryTaskID <> 235
                        AND LibraryTaskID <> 236
                        AND LibraryTaskID <> 237        
                        AND LibraryTaskID <> 238
                        AND LibraryTaskID <> 239
                        AND LibraryTaskID <> 240
                        AND LibraryTaskID <> 242
                        AND LibraryTaskID <> 243
                        AND LibraryTaskID <> 295
                        AND LibraryTaskID <> 299
                        AND LibraryTaskID <> 303
                        AND LibraryTaskID <> 304
                        AND LibraryTaskID <> 305
                        AND LibraryTaskID <> 313
                        AND LibraryTaskID <> 314
                        AND LibraryTaskID <> 321
                        AND LibraryTaskID <> 333

               )
               GROUP BY [Job_No]
               FOR XML PATH('Job_No'), ROOT('Root')

此查询返回此数据:

<Root>
  <Job_No Key="ORC0023">
    <baseOrSchedStartList>
      <string>2015-09-11T08:00:00</string>
    </baseOrSchedStartList>
    <baseOrSchedStartList>
      <string>2015-08-10T16:00:00</string>
    </baseOrSchedStartList>
    <baseOrSchedStartList>
      <string>2015-08-11T16:00:00</string>
    </baseOrSchedStartList>
  </Job_No>
</Root>

我正在寻找的是以下内容:

what I am looking for is the following:

<Root>
  <Job_No Key="ORC0023">
    <baseOrSchedStartList>
      <string>2015-09-11T08:00:00</string>
      <string>2015-08-10T16:00:00</string>
      <string>2015-08-11T16:00:00</string>
    </baseOrSchedStartList>
  </Job_No>
</Root>

任何帮助将不胜感激.

推荐答案

UPDATE 根据你的后续问题H3>

如果你从 AS [@Key] 中去掉 @ 你会得到这个

UPDATE According to your follow up question

If you just take away the @ from the AS [@Key] you'll get this

DECLARE @tbl TABLE([Key] VARCHAR(10),DateValue DATETIME);
INSERT INTO @tbl VALUES ('ORC0023','2015-09-11T08:00:00')
                       ,('ORC0023','2015-08-10T16:00:00')
                       ,('ORC0023','2015-08-11T16:00:00')

DECLARE @UniqueKey VARCHAR(10)='ORC0023';
SELECT @UniqueKey AS [Key]
      ,(
        SELECT DateValue AS [string]
        FROM @tbl AS tbl 
        WHERE tbl.[Key]=@UniqueKey
        FOR XML PATH(''),TYPE
       ) AS baseOrSchedStartList
FOR XML PATH('Job_No'),ROOT('Root')

/*
<Root>
  <Job_No>
    <Key>ORC0023</Key>
    <baseOrSchedStartList>
      <string>2015-09-11T08:00:00</string>
      <string>2015-08-10T16:00:00</string>
      <string>2015-08-11T16:00:00</string>
    </baseOrSchedStartList>
  </Job_No>
</Root>
*/

UPDATE2 对于您的实际查询,这意味着(可能)

尝试将 as '@Key' 更改为 as Key

        ORDER BY DATE_TO_END, SortOrder
        FOR XML PATH('baseOrSchedStartList'), Type
)

        ORDER BY DATE_TO_END, SortOrder
        FOR XML PATH(''), Type
) AS baseOrSchedStartList

另一种选择 - 正如 BateTech 在评论中指出的 - 将您的baseOrSchedStart"作为 ,ROOT('baseOrSchedStart') 放在 FOR XML PATH('') 并让括号未命名...

Another alternative was - as pointed out by BateTech in a comment - to put your "baseOrSchedStart" as ,ROOT('baseOrSchedStart') behind the FOR XML PATH('') and let the paranthesis unnamed...

如果没有您的表结构和测试数据,就很难完全理解您的查询(实际上不是超长 :-))

Without your table's structures and test data it is difficult to fully understand your query (which isn't that super long actually :-) )

因此我准备了一个简化的结构示例,希望您能理解该方法并将其转移到您的实际数据中

Therefore I prepared a simplified structure example and hope, that you can understand the approach and transfer this to your actual data

请尝试以下操作:

DECLARE @tbl TABLE([Key] VARCHAR(10),DateValue DATETIME);
INSERT INTO @tbl VALUES ('ORC0023','2015-09-11T08:00:00')
                       ,('ORC0023','2015-08-10T16:00:00')
                       ,('ORC0023','2015-08-11T16:00:00')

DECLARE @UniqueKey VARCHAR(10)='ORC0023';

--This is structurally what you've got
SELECT @UniqueKey AS [@Key]
      ,(
        SELECT DateValue AS [string]
        FROM @tbl AS tbl 
        WHERE tbl.[Key]=@UniqueKey
        FOR XML PATH('baseOrSchedStartList'),TYPE
       )
FOR XML PATH('Job_No'),ROOT('Root')

/*
<Root>
  <Job_No Key="ORC0023">
    <baseOrSchedStartList>
      <string>2015-09-11T08:00:00</string>
    </baseOrSchedStartList>
    <baseOrSchedStartList>
      <string>2015-08-10T16:00:00</string>
    </baseOrSchedStartList>
    <baseOrSchedStartList>
      <string>2015-08-11T16:00:00</string>
    </baseOrSchedStartList>
  </Job_No>
</Root>
*/

现在试试这个

SELECT @UniqueKey AS [@Key]
      ,(
        SELECT DateValue AS [*]
        FROM @tbl AS tbl 
        WHERE tbl.[Key]=@UniqueKey
        FOR XML PATH('string'),TYPE
       ) AS baseOrSchedStartList
FOR XML PATH('Job_No'),ROOT('Root')

/*
<Root>
  <Job_No Key="ORC0023">
    <baseOrSchedStartList>
      <string>2015-09-11T08:00:00</string>
      <string>2015-08-10T16:00:00</string>
      <string>2015-08-11T16:00:00</string>
    </baseOrSchedStartList>
  </Job_No>
</Root>
*/

顺便说一句:这和这个一样:

Btw: This is the same as this:

SELECT @UniqueKey AS [@Key]
      ,(
        SELECT DateValue AS [string]
        FROM @tbl AS tbl 
        WHERE tbl.[Key]=@UniqueKey
        FOR XML PATH(''),TYPE
       ) AS baseOrSchedStartList
FOR XML PATH('Job_No'),ROOT('Root')

这篇关于嵌套行的 SQL FOR XML 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Query with t(n) and multiple cross joins(使用 t(n) 和多个交叉连接进行查询)
Unpacking a binary string with TSQL(使用 TSQL 解包二进制字符串)
Max rows in SQL table where PK is INT 32 when seed starts at max negative value?(当种子以最大负值开始时,SQL 表中的最大行数其中 PK 为 INT 32?)
Inner Join and Group By in SQL with out an aggregate function.(SQL 中的内部连接和分组依据,没有聚合函数.)
Add a default constraint to an existing field with values(向具有值的现有字段添加默认约束)
SQL remove from running total(SQL 从运行总数中删除)