如果我没有正确地填写这个问题,我很抱歉,但是我无法从SQL查询中获得所需的结果。
select u.id as userid,
quiz.name as quiz_name,
[moodle_test].[dbo].mdl_question_attempts.questionusageid,
quiz_attempts.sumgrades as attempt_grade,
mdl_question_attempts.slot as question_number,
mdl_question_attempts.questionsummary as question,
mdl_question_attempts.rightanswer,
mdl_question_attempts.responsesummary
from [moodle_test].[dbo].mdl_user as u
inner join [moodle_test].[dbo].mdl_quiz_attempts as quiz_attempts on u.id = quiz_attempts.userid
inner join [moodle_test].[dbo].mdl_quiz as quiz on quiz.id = quiz_attempts.quiz
left join [moodle_test].[dbo].mdl_question_attempts on quiz_attempts.id = [moodle_test]. [dbo].mdl_question_attempts.questionusageid
where quiz.course = 2a
产生类似
的数据userid quiz_name questionusageid attempt_grade question_number question rightanswer responsesummary
180 Module 2 Pre-Test 285 6.00000 1 A properly structured team yields all of the following benefits, EXCEPT:: A leader is clearly identified; A clear plan of care; The patient is involved in the care process; Team members know their roles and responsibilities A clear plan of care The patient is involved in the care process
180 Module 2 Pre-Test 285 6.00000 2 A Contingency Team includes all of the following characteristics, EXCEPT:: It is formed for emergent or specific events; It is time-limited (e.g., Code Team, Disaster Response Team, Rapid Response Team); It is composed of team members drawn from a variety of Core Teams; It performs day-to-day operational management It performs day-to-day operational management It performs day-to-day operational management
180 Module 2 Pre-Test 285 6.00000 3 Team members include anyone who can take action in the process of patient care. Which of the following characteristics do team members share?: Roles and responsibilities that change; Individual goals that take priority over the team's mission; Accountability only to the team leader; A need to remain continually informed for effective team functioning A need to remain continually informed for effective team functioning A need to remain continually informed for effective team functioning
180 Module 2 Pre-Test 285 6.00000 4 Examples of effective strategies for involving patients in their care include all of the following, EXCEPT:: Reviewing the hospital bill with the patient at discharge; Conducting handoffs at the patient's bedside; Providing patients with tools for communicating with their care team; Including patients in bedside rounding Reviewing the hospital bill with the patient at discharge Reviewing the hospital bill with the patient at discharge
如何加入三个问题,系统答案和正确答案,以便用户的所有问题答案都在一行中?
答案 0 :(得分:0)
确定。这里的主要问题是我们无法知道将进行多少次尝试。 SQL没有简单的方法将可变数量的行转换为可变数量的列。因此,所有这些响应都必须连接成某种分隔的字符串并塞进一个字段中。 XPATH查询可以提供一种方法来实现此目的。
下面,CTE baseTable与您在上面提供的查询相同。然后,动态表Main保存基本结果,但是在answerList中有一个尾随分隔符(在这里使用管道" |" - 但你可以做任何事情)我们需要去除出。所以,这个第一个查询在桌子上#Main;'只有在那里切掉最后一个分隔符
下一个查询' outerQuery'标识唯一的列,即相同性'你不想重复。它会明确地选择这些行,因此您只能获得每个行中的一个结果集。基本上,您的用户/问题信息
' innerQuery'使用XML PATH将响应与管道分隔符一起压缩,根据我们在外部查询中识别的不同行将它们排成一行。
我希望这是有道理的;这是代码。未经测试。几乎可以保证包含至少一个拼写错误。希望它有所帮助。
WITH baseTable as
(
select u.id as userid,
quiz.name as quiz_name,
[moodle_test].[dbo].mdl_question_attempts.questionusageid,
quiz_attempts.sumgrades as attempt_grade,
mdl_question_attempts.slot as question_number,
mdl_question_attempts.questionsummary as question,
mdl_question_attempts.rightanswer,
mdl_question_attempts.responsesummary
from [moodle_test].[dbo].mdl_user as u
inner join [moodle_test].[dbo].mdl_quiz_attempts as quiz_attempts on u.id = quiz_attempts.userid
inner join [moodle_test].[dbo].mdl_quiz as quiz on quiz.id = quiz_attempts.quiz
left join [moodle_test].[dbo].mdl_question_attempts on quiz_attempts.id = [moodle_test]. [dbo].mdl_question_attempts.questionusageid
where quiz.course = 2a
)
Select Main.userid, Main.quiz_name, Main.questionusageid, Main.attemmpt_grade, Main.question_number, Main.question, Main.rightanswer,
Left(Main.answerList,Len(Main.answerList)-1) As answerList
From
(
Select distinct userid, quiz_name, questionusageid, attempt_grade, question_number, question, rightanswer,
(
SELECT innerQuery.responsesummary + '|' AS [text()]
FROM baseTable innerQuery
WHERE innerQuery.userid = outerQuery.userid
AND innerQuery.questionusageid = outerQuery.questionusageid
AND innerQuery.question_number= outerQuery.question_number
ORDER BY innerQuery.userid, innerQuery.questionusageid, innerQuery.question_number
FOR XML PATH ('')
) answerList
From baseTable outerQuery
)[Main]