How to select the same field twice with different conditions and display results as separate fields

后端 未结 3 585
渐次进展
渐次进展 2021-01-24 23:56

I want to create a Table Adapter in C# as follows.

I wish to use same filed in a table twice with different conditions, and it must be shown as two diff

相关标签:
3条回答
  • 2021-01-25 00:23
     SELECT A.StudentID, A.Studname, A.StudSex, A.StudCourse, 
    SUM(CASE WHEN A.AttendStatus = 'Present' THEN 1 ELSE 0 END) AS TotalPresent,
    SUM(CASE WHEN A.AttendStatus = 'Absent' THEN 1 ELSE 0 END) AS TotalAbsent
    FROM AttendanceReg A 
    INNER JOIN LocalTable L ON A.StudCourse = L.AttendCourse AND DatePart('M', A.DateOfAttendance) = L.AttentMonth)
    
    GROUP BY A.StudentID, A.Studname, A.StudSex, A.StudCourse
    
    0 讨论(0)
  • 2021-01-25 00:24

    Finally I have Solved This Issue, Thanks To @Ikram Turgunbaev :)

    Here Is the Code

    SELECT AttendanceReg.StudentID,AttendanceReg.Studname, AttendanceReg.StudSex,
    AttendanceReg.StudCourse,
    SUM(IIf( AttendanceReg.AttendStatus = 'Present', 1,0)) AS TotalPresent,
    SUM(IIf( AttendanceReg.AttendStatus = 'Absent', 1,0)) AS TotalAbsent, 
    DatePart('m', AttendanceReg.DateOfAttendance) AS MYMONTH
    
    FROM (AttendanceReg
        INNER JOIN LocalTable ON
        AttendanceReg.StudCourse = LocalTable.AttendCourse 
        AND 
        DatePart('m', AttendanceReg.DateOfAttendance) = LocalTable.AttentMonth)
    
    GROUP BY AttendanceReg.StudentID, AttendanceReg.Studname,
        AttendanceReg.StudSex, AttendanceReg.StudCourse,
        DatePart('m', AttendanceReg.DateOfAttendance)
    
    ORDER BY DatePart('m', AttendanceReg.DateOfAttendance) DESC
    
    0 讨论(0)
  • 2021-01-25 00:40

    The Same thing Can also be done using Switch :)

    SELECT EmpAttendance.EmpID, EmpAttendance.Empname, EmpAttendance.EmpSex, EmpAttendance.EmpDepartment, EmpProfileTBL.Salary, 
    SUM(SWITCH(EmpAttendance.AttendStatus = 'Present', 1, EmpAttendance.AttendStatus = 'Absent', 0, EmpAttendance.AttendStatus = 'Half-Day', 0)) AS TotalPresent,
    SUM(SWITCH(EmpAttendance.AttendStatus = 'Absent', 1, EmpAttendance.AttendStatus = 'Present', 0, EmpAttendance.AttendStatus = 'Half-Day', 0)) AS TotalAbsent,
    SUM(SWITCH(EmpAttendance.AttendStatus = 'Half-Day', 1, EmpAttendance.AttendStatus = 'Present', 0, EmpAttendance.AttendStatus = 'Absent', 0)) AS HalfDay,
    
    DatePart('m', EmpAttendance.DateOfAttendance) AS CurMonth,
    ((EmpProfileTBL.Salary * TotalPresent) - (EmpProfileTBL.Salary * TotalAbsent) 
                             + ((EmpProfileTBL.Salary / 2) * HalfDay)) AS FinalSalary
    FROM ((EmpAttendance INNER JOIN LocalVariableTable
    ON EmpAttendance.EmpDepartment = LocalVariableTable.TempDepartment AND
    DatePart('m', EmpAttendance.DateOfAttendance) = LocalVariableTable.TempMonth
     AND DatePart('yyyy', EmpAttendance.DateOfAttendance) = LocalVariableTable.TempYear)
    INNER JOIN
    EmpProfileTBL ON EmpAttendance.EmpID = EmpProfileTBL.EmpID)
    
    GROUP BY EmpAttendance.EmpID, EmpAttendance.Empname, EmpAttendance.EmpSex,
    EmpAttendance.EmpDepartment, DatePart('m', EmpAttendance.DateOfAttendance), 
    EmpProfileTBL.Salary
    ORDER BY DatePart('m', EmpAttendance.DateOfAttendance) DESC
    
    0 讨论(0)
提交回复
热议问题