vb.net sql查询计数

时间:2015-08-05 08:39:07

标签: mysql sql vb.net

我有一个包含3列(板,线,缺陷)的SQL数据库

当前的sql查询计算同一行上同一块板上的所有缺陷总数,并填充数据网格视图。

sqlCmd.CommandText = "SELECT board, count(defect) AS defect FROM [sqlccmdefects] WHERE [line] = @line GROUP BY board ORDER BY defect DESC" ' and date >= @startdata  AND date <= @enddata 

'SQL Command Params
sqlCmd.Parameters.Add("@line", SqlDbType.VarChar, 30).Value = mainform.line.Text
sqlCmd.Parameters.Add("@board", SqlDbType.VarChar, 30).Value = mainform.boardname.Text

“缺陷”列的值为“污染”或“违规”。我如何改变查询以计算同一板和同一行的“污染”数量,我搜索谷歌并且大脑无效。

1 个答案:

答案 0 :(得分:0)

这里有两个选项:

1 - 缺陷分组:

SELECT board, count(defect) AS defect FROM [sqlccmdefects] WHERE [line] = @line 
GROUP BY board, defect 
ORDER BY defect DESC

2 - 将缺陷添加到where子句:

SELECT board, count(defect) AS defect FROM [sqlccmdefects] WHERE [line] = @line 
and defect = 'contamination'
GROUP BY board, defect 
ORDER BY defect DESC