【发布时间】:2018-06-08 18:19:37
【问题描述】:
我有这个问题:
;WITH cte AS
(
SELECT
US.Description,
US.Title Summary,
ISNULL(US.MyWizardUserStoryId,'')UserStoryID,
A.RequirementId,ISNULL(US.ProjectId,'')ProjectId,
--(case when A.AnalysisOutcome='Found' then 'Ambiguous' else 'Non Ambiguous' end)
CASE WHEN ISNULL(A.AnalysisOutcome,'')='' THEN 'Analysis Pending'
WHEN ISNULL(A.AnalysisOutcome,'')='Found' THEN 'Ambiguities found'
WHEN ISNULL(A.AnalysisOutcome,'')='Not Found' THEN 'No Ambiguities'
WHEN ISNULL(A.AnalysisOutcome,'')='Submitted' THEN 'Submitted'
WHEN ISNULL(A.AnalysisOutcome,'')='Rejected' THEN 'Rejected'
WHEN (ISNULL(A.AnalysisOutcome,'')='Internal Processing Failure'
OR ISNULL(A.AnalysisOutcome,'')='Requirement Not Parsable')
THEN 'Analysis failure'
END as AnalysisOutcome,
US.AssignedTo,
ISNULL(AD.RequirementAnalysisId,'')RequirementAnalysisId,
AD.VagueWord,
AD.ContentText,
AD.Explanation,
AD.Suggestion,
AD.SuggestionList,
US.Title Summary2,
AD.VaguePhrase,
AD.VagueTypeText
,AD.VagueWord VagueWord2
,US.ModifiedOn
FROM (RequirementAnalysis A
RIGHT JOIN UserStory US ON
A.RequirementId=US.MyWizardUserStoryId)
left JOIN
AmbiguityAnalysisResult AD ON
A.RequirementAnalysisId=AD.RequirementAnalysisId
AND
A.RequirementAnalysisId=(SELECT MAX(RequirementAnalysisId) FROM RequirementAnalysis WHERE RequirementId=A.RequirementId
AND ProjectId=A.ProjectId)
)
SELECT UserStoryId,
[Description],
Summary,
UserStoryID,
ProjectId,
AnalysisOutcome,
AssignedTo,
RequirementAnalysisId,
RequirementId,
(SELECT distinct VagueWord
FROM cte
WHERE UserStoryId = c.UserStoryId
FOR XML PATH('VagueWordsArray'),TYPE)
,
(SELECT ContentText,Explanation,Suggestion,
SuggestionList,
Summary,VaguePhrase,VagueTypeText,VagueWord
FROM cte
WHERE UserStoryId = c.UserStoryId
FOR XML PATH('SuggestionData'),TYPE)
FROM cte c
WHERE UserStoryId in (141204,1400235)
GROUP BY UserStoryId,
[Description],
Summary,
UserStoryID,
ProjectId,
AnalysisOutcome,
AssignedTo,
RequirementAnalysisId,
RequirementId
FOR XML PATH('StoryData'),ROOT('xml'),TYPE
此查询返回此 XML:
<xml>
<StoryData>
<UserStoryId>141204</UserStoryId>
<Description>Customer can see the applicable discount on the quote and change in premium.</Description>
<Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
<UserStoryID>141204</UserStoryID>
<ProjectId>23040</ProjectId>
<AnalysisOutcome>Ambiguities found</AnalysisOutcome>
<AssignedTo>ankit.k.arora</AssignedTo>
<RequirementAnalysisId>5901</RequirementAnalysisId>
<RequirementId>141204</RequirementId>
<VagueWordsArray>
<VagueWord>and</VagueWord>
</VagueWordsArray>
<VagueWordsArray>
<VagueWord>applicable</VagueWord>
</VagueWordsArray>
<SuggestionData>
<ContentText>Customer can see the applicable discount on the quote and change in premium.</ContentText>
<Explanation>Suggests that you are combining requirements. Requirements that contain conjunctions/disjunctions (AND/OR) are dangerous and can lead to downstream problems in defining scope of the requirement.</Explanation>
<Suggestion>Keep each requirement in a single sentence.</Suggestion>
<Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
<VagueTypeText>Not Standard</VagueTypeText>
<VagueWord>and</VagueWord>
</SuggestionData>
<SuggestionData>
<ContentText>Customer can see the applicable discount on the quote and change in premium.</ContentText>
<Explanation>Can be interpreted in different ways by different people.</Explanation>
<Suggestion>Include the precise definition in the glossary.</Suggestion>
<Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
<VaguePhrase>applicable discount</VaguePhrase>
<VagueTypeText>Not Defined</VagueTypeText>
<VagueWord>applicable</VagueWord>
</SuggestionData>
</StoryData>
<StoryData>
<UserStoryId>1400235</UserStoryId>
<Description>story 1</Description>
<Summary>undefined</Summary>
<UserStoryID>1400235</UserStoryID>
<ProjectId>23040</ProjectId>
<AnalysisOutcome>Rejected</AnalysisOutcome>
<AssignedTo>anmol.aggarwal@accenture.com</AssignedTo>
<RequirementAnalysisId>5958</RequirementAnalysisId>
<RequirementId>1400235</RequirementId>
<VagueWordsArray>
<VagueWord>and</VagueWord>
</VagueWordsArray>
<VagueWordsArray>
<VagueWord>or</VagueWord>
</VagueWordsArray>
<VagueWordsArray>
<VagueWord>should</VagueWord>
</VagueWordsArray>
<SuggestionData>
<ContentText>Story should or and not fail</ContentText>
<Explanation>Suggests that you are combining requirements. Requirements that contain conjunctions/disjunctions (AND/OR) are dangerous and can lead to downstream problems in defining scope of the requirement.</Explanation>
<Suggestion>Keep each requirement in a single sentence.</Suggestion>
<SuggestionList />
<Summary>undefined</Summary>
<VagueTypeText>Not Standard</VagueTypeText>
<VagueWord>and</VagueWord>
</SuggestionData>
<SuggestionData>
<ContentText>Story should or and not fail</ContentText>
<Explanation>Suggests that you are combining requirements. Requirements that contain conjunctions/disjunctions (AND/OR) are dangerous and can lead to downstream problems in defining scope of the requirement.</Explanation>
<Suggestion>Keep each requirement in a single sentence.</Suggestion>
<SuggestionList />
<Summary>undefined</Summary>
<VagueTypeText>Not Standard</VagueTypeText>
<VagueWord>or</VagueWord>
</SuggestionData>
<SuggestionData>
<ContentText>Story should or and not fail</ContentText>
<Explanation>Is often ambiguous, or inappropriate. Some readers will interpret these as optional or advisory, others as required.</Explanation>
<Suggestion>Use 'shall/must/will' for requirements,</Suggestion>
<SuggestionList>shall,must,will</SuggestionList>
<Summary>undefined</Summary>
<VagueTypeText>Not Standard</VagueTypeText>
<VagueWord>should</VagueWord>
</SuggestionData>
</StoryData>
</xml>
我正在尝试将其转换为 FOR JSON 查询,但出现此错误:
消息 13605,第 16 级,状态 1,第 1 行
没有名称或别名的列表达式和数据源不能使用 FOR JSON 子句格式化为 JSON 文本。为未命名的列或表添加别名。
我尝试过以下 Json 查询:
;WITH cte AS(
SELECT
US.Description
,US.Title Summary
,ISNULL(US.MyWizardUserStoryId,'')UserStoryID
,A.RequirementId,ISNULL(US.ProjectId,'')ProjectId,
--(case when A.AnalysisOutcome='Found' then 'Ambiguous' else 'Non Ambiguous' end)
CASE WHEN ISNULL(A.AnalysisOutcome,'')='' THEN 'Analysis Pending'
WHEN ISNULL(A.AnalysisOutcome,'')='Found' THEN 'Ambiguities found'
WHEN ISNULL(A.AnalysisOutcome,'')='Not Found' THEN 'No Ambiguities'
WHEN ISNULL(A.AnalysisOutcome,'')='Submitted' THEN 'Submitted'
WHEN ISNULL(A.AnalysisOutcome,'')='Rejected' THEN 'Rejected'
WHEN (ISNULL(A.AnalysisOutcome,'')='Internal Processing Failure'
OR ISNULL(A.AnalysisOutcome,'')='Requirement Not Parsable')
THEN 'Analysis failure'
END as AnalysisOutcome,
US.AssignedTo,
ISNULL(AD.RequirementAnalysisId,'')RequirementAnalysisId,
AD.VagueWord,
AD.ContentText,
AD.Explanation,
AD.Suggestion,
AD.SuggestionList,
AD.VaguePhrase,
AD.VagueTypeText
,US.ModifiedOn
FROM (RequirementAnalysis A
RIGHT JOIN UserStory US ON
A.RequirementId=US.MyWizardUserStoryId)
left JOIN
AmbiguityAnalysisResult AD ON
A.RequirementAnalysisId=AD.RequirementAnalysisId
AND
A.RequirementAnalysisId=(SELECT MAX(RequirementAnalysisId) FROM RequirementAnalysis WHERE RequirementId=A.RequirementId
AND ProjectId=A.ProjectId)
)
SELECT UserStoryId,
[Description],
Summary,
UserStoryID,
ProjectId,
AnalysisOutcome,
AssignedTo,
RequirementAnalysisId,
RequirementId
,
(SELECT distinct VagueWord
FROM cte
WHERE UserStoryId = c.UserStoryId
FOR Json PATH,ROOT('VagueWordsArray'))
,
(SELECT ContentText,Explanation,Suggestion,
SuggestionList,
Summary,VaguePhrase,VagueTypeText,VagueWord
FROM cte
WHERE UserStoryId = c.UserStoryId
FOR Json PATH,ROOT('SuggestionData'))
FROM cte c
WHERE UserStoryId in (141204,1400235)
GROUP BY UserStoryId,
[Description],
Summary,
UserStoryID,
ProjectId,
AnalysisOutcome,
AssignedTo,
RequirementAnalysisId,
RequirementId
FOR Json PATH,ROOT('StoryData')
【问题讨论】:
-
首先,FOR JSON 仅适用于 MSSQL 2016 及更高版本。其次,请确认您对所有显示的列都有别名
标签: sql json sql-server xml