How to write Query

Hello,

please see the output of my query shown bellow

Name employeeID FirstName LastName Attempt score Attempt Score % Required Passing % Attempt status sectionID questionID question questiontype Correct_Answer Learner_Answer Evaluation resultOfQ Answer_Score
oracle 12 a a 2 100 80 Passed 16 27 a MCQ Multimedia Multimedia started correct 1
oracle 12 a a 2 100 80 Passed 16 27 b MCQ Multimedia Multimedia started correct 1
oracle 12 a a 0 0 80 Incomplete 16 27 c MCQ Multimedia Multimedia started correct 1
oracle 12 a a 2 100 80 Passed 16 26 d MCQ network network started correct 1

But I want the output as shown bellow. Please help me.

Name employeeID FirstName LastName Attempt score Attempt Score % Required Passing % Attempt status sectionID questionID question questiontype Correct_Answer Learner_Answer Evaluation resultOfQ Answer_Score question questiontype Correct_Answer Learner_Answer Evaluation resultOfQ Answer_Score question questiontype Correct_Answer Learner_Answer Evaluation resultOfQ Answer_Score question questiontype Correct_Answer Learner_Answer Evaluation resultOfQ Answer_Score
oracle 12 a a 2 100 80 Passed 16 27 a MCQ Multimedia Multimedia started correct 1 b MCQ Multimedia Multimedia started correct 1 c MCQ Multimedia Multimedia started correct 1 d MCQ network network started correct 1

June 18th, 2014 9:53am

You should take a look at the PIVOT / UNPIVOT syntax, this will help you get started:

http://technet.microsoft.com/en-us/library/ms177410(v=SQL.105).aspx

-Jens

Free Windows Admin Tool Kit Click here and download it now
June 18th, 2014 10:46am

Hi

According to your description, when executing a query in SQL Server, the column name cannot be the same, we can use alias instead.

In addition, we need to verify if these fields you displayed are from different tables or just one table. If they are from different tables, we need to know which tables this query involves, the join relationships between tables for further analysis. However, if they are contained in one table, you can try the PIVOT / UNPIVOT syntax as Jenss post, or try the JOIN syntax as follows:

use TestDb 
go
select 
      A.Name,
      A.employeeID,
      A.FirstName,
      A.LastName,
      A.Attemptscore,
      A.AttemptscorePer,
      A.RequiredPassPer,
      A.Attemptstatus,
      A.sectionID,
      A.questionID,
      A.question,
      A.questiontype,
      A.Correct_Answer,
      A.Learner_Answer,
      A.Evaluation,
      A.resultOfQ,
      A.Answer_Score,

      B.b_question,
      B.b_questiontype,
      B.b_Correct_Answer,
      B.b_Learner_Answer,
      B.b_Evaluation,
      B.b_resultOfQ,
      B.b_Answer_Score,

      C.c_question,
      C.c_questiontype,
      C.c_Correct_Answer,
      C.c_Learner_Answer,
      C.c_Evaluation,
      C.c_resultOfQ,
      C.c_Answer_Score,

      D.d_question,
      D.d_questiontype,
      D.d_Correct_Answer,
      D.d_Learner_Answer,
      D.d_Evaluation,
      D.d_resultOfQ,
      D.d_Answer_Score
from dbo.WriteQuery  A  
 join
  (select
       employeeID 
      ,question b_question
      ,questiontype b_questiontype
      ,Correct_Answer b_Correct_Answer
      ,Learner_Answer b_Learner_Answer
      ,Evaluation b_Evaluation
      ,resultOfQ b_resultOfQ
      ,Answer_Score b_Answer_Score
   from dbo.WriteQuery where dbo.WriteQuery.question='b') B
   on A.employeeID=B.employeeID 
 join
  (select
       employeeID 
      ,question c_question
      ,questiontype c_questiontype
      ,Correct_Answer c_Correct_Answer
      ,Learner_Answer c_Learner_Answer
      ,Evaluation c_Evaluation
      ,resultOfQ c_resultOfQ
      ,Answer_Score c_Answer_Score
   from dbo.WriteQuery where dbo.WriteQuery.question='c') C
   on A.employeeID=C.employeeID 
 join
  (select
       employeeID 
      ,question d_question
      ,questiontype d_questiontype
      ,Correct_Answer d_Correct_Answer
      ,Learner_Answer d_Learner_Answer
      ,Evaluation d_Evaluation
      ,resultOfQ d_resultOfQ
      ,Answer_Score d_Answer_Score
   from dbo.WriteQuery where dbo.WriteQuery.question='d') D
  on A.employeeID=D.employeeID 
  where A.question ='a' 

Thanks
Lydia Zhang

June 19th, 2014 3:38am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics