如何以不同的条件选择相同的字段两次并将结果显示为单独的字段

Posted

技术标签:

【中文标题】如何以不同的条件选择相同的字段两次并将结果显示为单独的字段【英文标题】:How to select the same field twice with different conditions and display results as separate fields 【发布时间】:2017-01-24 19:30:35 【问题描述】:

我想在 C# 中创建一个 Table Adapter,如下所示。

我希望在一个表中两次使用不同条件下的同一个字段,并且它必须显示为两个不同的字段。

这是我的源表:

AttendanceReg 表中,我需要Count 基于AttendStatus 的每个学生的记录,以根据course and month 找到他们在Absent and present 的天数。

示例查询:

Select  AttendanceReg.StudentID
      , AttendanceReg.Studname
      , AttendanceReg.StudSex
      , AttendanceReg.StudCourse
      , Count(AttendanceReg.AttendStatus) As Total_Present
      , DatePart('m', AttendanceReg.DateOfAttendance) As Month
From    (AttendanceReg
         Inner Join LocalTable
            On AttendanceReg.StudCourse = LocalTable.AttendCourse
               And DatePart('M', AttendanceReg.DateOfAttendance) = LocalTable.AttentMonth
        )
Where   (AttendanceReg.AttendStatus = 'Present')
Group By AttendanceReg.StudentID
      , AttendanceReg.Studname
      , AttendanceReg.StudSex
      , AttendanceReg.StudCourse
      , DatePart('m', AttendanceReg.DateOfAttendance)
Order By DatePart('m', AttendanceReg.DateOfAttendance) Desc

查询结果和期望的结果(第一张图片显示了我得到的结果,第二张图片显示了我真正想要的结果):

我需要做什么?

注意:我需要使用LocalTable,我不能传入参数来指定CourseMonth

【问题讨论】:

【参考方案1】:
 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

【讨论】:

执行时出现错误..无法解析查询文本。查询无法在图表和标准窗格中以图形方式表示。 我正在使用 Microsoft Access Database 2010,我认为 access 数据库不支持“CASE WHEN” SELECT StudentID,Studname,StudSex,StudCourse, SUM(IIf(AttendStatus = 'Present', 1,0)) AS TotalPresent, SUM(IIf(AttendStatus = 'Absent', 1,0)) AS TotalAbsent FROM AttendanceReg GROUP BY StudentID,Studname,StudSex,StudCourse 这几乎可以工作......现在我如何使用 where 条件来查询? 可以在group by子句前添加where查询【参考方案2】:

我终于解决了这个问题,感谢@Ikram Turgunbaev :)

这是代码

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

【讨论】:

【参考方案3】:

同样的事情也可以使用 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

【讨论】:

以上是关于如何以不同的条件选择相同的字段两次并将结果显示为单独的字段的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Mongo 存储库两次查询同一字段?

Spring Mongo 条件查询两次相同的字段

SQL一次性查询一个字段不同条件下的统计结果

如何通过不同的 csv 文件迭代选择的操作并将结果输出到新的 csv 文件

绘图图例显示未知项目/相同的图例项目以不同的线条显示两次

MySQL 从一张表查询 - 选择相同的字段两次