UserGroup by&order by筛选MySQL查询

时间:2018-11-24 15:31:51

标签: mysql

我编写了一个查询,以汇总一列中的绿色状态和红色状态,并根据节点名称对它们进行分组。

Dim cmd As New OleDbCommand
       Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\your_path\DB.accdb")

       Dim queryResult As Integer
       Dim sqlQRY As String = "SELECT COUNT(*) AS FROM ESRRegister WHERE ID = '" & IDtxt.Text & "'"
       con.Open()
       cmd.Connection = con
       cmd.CommandType = CommandType.Text
       If queryResult > 0 Then
           cmd.CommandText = "insert into ESRRegister (Dt,ID)VALUES ('" & Dttxt.Text & "' , '" & IDtxt.Text & "')"
           queryResult = cmd.ExecuteScalar()
           MsgBox("Added Successfuly")
       Else

           MessageBox.Show("Already Exists!", "ALI ENTERPRISES", MessageBoxButtons.OK, MessageBoxIcon.Information)

       End If

如何通过过滤Red为0的结果(我不想显示它是否为0,因为不需要注意)并根据Reds计数按Desc顺序进行排序来使查询更好?

谢谢

1 个答案:

答案 0 :(得分:0)

只需回答有关过滤Reds = 0和按Reds计数排序desc的问题即可:

SELECT `Node`, Date(`DOR`) as "Date of Report",
    SUM(CASE WHEN `Color` = 'Green' THEN 1 ELSE 0 END) as Pass
 , SUM(CASE WHEN `Clor` = 'Red' THEN 1 ELSE 0 END) as Fail
 , SUM(CASE WHEN `Color` = 'Green' or `Color` = 'Red' THEN 1 ELSE 0 END) as Total Checks
FROM Monitor Where date(`TOI`) >= DATE(NOW()) - INTERVAL 7 DAY
Group by `Server`
HAVING Fail > 0
order by Reds desc

但是,正如Tim所言,您在查询中遇到问题,例如,您正在按服务器分组,但是随后选择NodeDOR

尝试包括表结构和示例数据,以便我们能够更好地理解问题,从而为您提供正确的答案