我有3列(板,线,缺陷)的sql数据库
当前的sql查询会计算同一行同一条板上的所有缺陷总数,并填充一个datagrid视图。
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
“缺陷”列的值为“污染”或“违规”。我将如何更改查询以针对同一块板和同一行计算“污染”量,我已经搜索过Google,但大脑却无法正常工作。
最佳答案
这里有两个选项供您选择:
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