无法对 xml 路径进行 GROUP BY 或 DISTINCT 我的查询

Cannot GROUP BY or DISTINCT my QUERY for xml path

我正在从我的 table 中提取章节标题、章节标题和问题来制作一个 table 内容。我正在使用 Caspio 的低代码平台,它在 SQL-Server.

上运行

我已经能够 GROUP BY 或 ORDER BY,但我似乎不能两者兼顾。就我而言,ORDER BY 更为重要。 GROUP BY 只会让它更容易处理。当我添加 GROUP BY 语句时,它什么也没显示。

我希望能够使用 GROUP BY 或使用 DISTINCT,您知道为什么不能吗?

这是我的查询:

SELECT [@field:WF_tbl_Chapter_Title] as Chapter, [@field:WF_tbl_Section_Title] as Section, [@field:WF_tbl_Question_Description] as Question
FROM _v_Questions_View as QV
WHERE [@field:WF_tbl_Workbook_Author_ID] = '[@authfield:WF_tbl_Customer_Customer_ID]' 
AND [@field:WF_tbl_Workbook_Workbook_ID] = '[@WID] '
ORDER BY QV.WF_tbl_Chapter_Position, QV.WF_tbl_Section_Position, QV.WF_tbl_Question_Position

for xml path(N'')

这是一个查询失败的例子:

SELECT [@field:WF_tbl_Chapter_Title] as Chapter, [@field:WF_tbl_Section_Title] as Section, [@field:WF_tbl_Question_Description] as Question
FROM _v_Questions_View as QV
WHERE [@field:WF_tbl_Workbook_Author_ID] = '[@authfield:WF_tbl_Customer_Customer_ID]' 
AND [@field:WF_tbl_Workbook_Workbook_ID] = '[@WID]'
GROUP BY [@field:WF_tbl_Chapter_Title], [@field:WF_tbl_Section_Title], [@field:WF_tbl_Question_Description]
ORDER BY QV.WF_tbl_Chapter_Position, QV.WF_tbl_Section_Position, QV.WF_tbl_Question_Position
for xml path(N'')

在 sql 服务器中搜索 Stack Overflow 以了解关于“for xml”的不同问题。 Caspio 使用 sql 服务器。

答案通常建议在子查询或 CTE 中隔离初始 select。

所以猜一猜....将您的工作查询包装成以下之一:

SELECT DISTINCT Chapter, Section, Question FROM
(SELECT [@field:WF_tbl_Chapter_Title] as Chapter, [@field:WF_tbl_Section_Title] as Section, [@field:WF_tbl_Question_Description] as Question
 FROM _v_Questions_View as QV
 WHERE [@field:WF_tbl_Workbook_Author_ID] = '[@authfield:WF_tbl_Customer_Customer_ID]' 
 AND [@field:WF_tbl_Workbook_Workbook_ID] = '[@WID] '
 ) AS SUBQ
 ORDER BY Chapter, Section, Question
for xml path(N'')

WITH X AS
    (SELECT [@field:WF_tbl_Chapter_Title] as Chapter, [@field:WF_tbl_Section_Title] as Section, [@field:WF_tbl_Question_Description] as Question
     FROM _v_Questions_View as QV
     WHERE [@field:WF_tbl_Workbook_Author_ID] = '[@authfield:WF_tbl_Customer_Customer_ID]' 
     AND [@field:WF_tbl_Workbook_Workbook_ID] = '[@WID] '
     GROUP BY [@field:WF_tbl_Chapter_Title], [@field:WF_tbl_Section_Title], [@field:WF_tbl_Question_Description]
     )
SELECT Chapter, Section, Question
FROM X
ORDER BY  Chapter, Section, Question
for xml path(N'')