Sql 将多个列实例合并到一行中

Sql 将多个列实例合并到一行中,sql,sql-server,Sql,Sql Server,如果我没有正确地表达这个问题,我很抱歉,但是我在从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_nu

如果我没有正确地表达这个问题,我很抱歉,但是我在从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

如何将问题、系统答案和正确答案三者结合起来,使用户的所有问题答案都排在一行?

好的。这里的主要问题是,我们不知道会进行多少次尝试。SQL没有简单的方法将可变数量的行转换为可变数量的列。因此,所有这些响应都必须连接到某种分隔字符串中,并填充到单个字段中。XPATH查询可以提供一种实现这一点的方法

下面,CTE baseTable与上面提供的查询相同。然后,动态表Main保存基本结果,但是这里有一个使用管道的尾随分隔符|-但是您可以在answerList中执行我们需要删除的任何操作。因此,表“Main”上的第一个查询仅用于切掉最后一个分隔符

下一个查询“outerQuery”标识唯一的列,即您不希望重复的“相同性”。它会清楚地选择这些行,这样您就可以得到一个结果集,其中每个行只有一个。基本上,您的用户/问题信息

“innerQuery”使用XML路径将响应与管道分隔符一起挤压,并根据我们在外部查询中标识的不同行将它们排列起来

我希望这是有道理的;这是密码。未经测试。几乎可以保证包含至少一个打字错误。希望能有帮助

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]

当前的输出是什么?我认为上面列出的结果是需要的?您是否试图将同一列中的多个字段合并到一个字段中?是否需要google SQL PIVOT?使用别名时应保持一致。你有时使用它们,而不是其他。这使得密码很难破译。顺便说一句,不推荐使用列列表中的4部分命名。将来你将被迫使用别名。所以。。。我对球门还是有点迷茫。你是说你想看到上面显示的四行压缩成一行,问题编号为1、2、3和4,以及他们的回答都在同一行吗?