简体   繁体   中英

How to add data from different rows and display them as columns of one row in sql query

I am new in the SQL query field but I can do some queries. I would like to add related data in a table rows and display them in the same column. Here is the table containing the data:

在此处输入图片说明

Any help will be appreciated. Thanks in advance

If there are actually only 3 Questions and assuming the table was named Student you could do something like this:

select studentid AS Student,
(select Questionpoints from Student where studentid = a.studentid and Questionid = 8) AS Qn1,
(select Questionpoints from Student where studentid = a.studentid and Questionid = 9) AS Qn2,
(select Questionpoints from Student where studentid = a.studentid and Questionid = 10) AS Qn3
from Student as a
group by studentid;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM