如何求和COUNT列并在Label中显示答案

时间:2018-03-18 15:18:01

标签: mysql vb.net

我有以下代码

 Dim cmd As New MySqlCommand("SELECT IdNumber, COUNT(DISTINCT date) AS 'Attendance' FROM record WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) GROUP BY idNumber ", cn)

    Dim i As MySqlDataReader = cmd.ExecuteReader()
    While i.Read
        Label1.Text = i("SUM(Attendance)")
        i.Close()
    End While

我需要在标签中显示COUNT列SUM,但是当我运行代码时,我收到以下错误:

  

'无法在结果中找到指定的列:SUM(出勤)'

在代码中生成COUNT列(出勤)以在特定日期期间获取表记录中的特定COUNT项并返回以下内容:

+----------+------------+
| idNumber | Attendance |
+-----------------------+
| 88383    | 4          |
| 64785    | 2          |
+-----------------------+

现在我想要对这些特定项目进行SUM并将它们显示在如下表格中:

标签= 6

1 个答案:

答案 0 :(得分:0)

如果您只需要总结每个IdNumber的所有不同日期,则无需选择idNumber,然后执行子查询以总结所有出勤率。

SELECT SUM(Attendance) as Label
from (
    SELECT COUNT(DISTINCT date) AS 'Attendance' 
    FROM record 
    WHERE MONTH(date) = MONTH(CURRENT_DATE()) 
    AND YEAR(date) = YEAR(CURRENT_DATE()) 
    GROUP BY idNumber
     );

 Result:
   Label
   6