显示参加多个科目的学生

时间:2017-12-18 10:00:03

标签: sql sql-server

我们说我有一张这样的桌子。

StudentID | SubCode | SubName
-----------------------------
    1          M1       Math
    1          S1       Science
    1          E1       English
    2          H1       History
    2          G2       Geography
    4          M1       Math  

我想要一个显示如下数据的查询..

StudentID | SubCode | SubName
-----------------------------
    1          M1       Math
               S1       Science
               E1       English
    2          H1       History
               G2       Geography
    4          M1       Math  

编辑:

这是我创建的查询,请查看您是否了解某些内容。非常感谢您的回复

SELECT 
 EI.[EmpCode], EI.[FirstName]+' '+EI.[LastName] [EmployeeName], EI.[FatherName], Dpt.[FullName] [Department],
 [Desig].[FullName] [Designation], EI.DOB [DateOfBirth], EI.DOJ [JoiningDate], EI.NCNIC [CNIC], EI.Mobile, EI.OfficialEmail,
 EI.Email, EI.Address, [Status].FullName [Status], [Company].FullName [Company], EI.EmpImage [Image], 
 [Academics].MajorSubjects, [Academics].DegreeInstituteName, [Academics].PassingYear, [Academics].CGPA, [Degree].[FullName] [DegreeName],
    CONVERT(varchar(3),DATEDIFF(MONTH, DOJ, GETDATE())/12) +' Year(s) '+
    CONVERT(varchar(2),DATEDIFF(MONTH, DOJ, GETDATE()) % 12)+ ' Month(s)' 
    AS [EXPERIENCE]


FROM [HRM].[tbl_EmployeeInfo] EI,  [HRM].[tbl_Designation] [Desig], HRM.tbl_Department [Dpt], 
[HRM].[tbl_EmployeeStatus] [Status], [AppCNF].[tbl_OrganizationCompany] [Company], [HRM].[tbl_Degree] [Degree],
[HRM].[tbl_EmployeeAcademic] [Academics]
WHERE [Desig].[ID] = EI.DesignationCode
AND
Dpt.ID = EI.[DeptCode]
AND
[Status].ID = EI.EmpStatusCode
AND
[Company].ID = EI.EmpCompanyCode
AND
[EI].ID = [Academics].EmpCode
AND
[Degree].ID = [Academics].[DegreeCode]
ORDER BY 1,4

1 个答案:

答案 0 :(得分:0)

试试这个:

StudentId   SubCode SubName
1           M1      Math
            S1      Science
            E1      English
2           H1      History
            G2      Geography
4           M1      Math

<强>输出:

{{1}}