如何从sql表中选择最高优先级

时间:2015-05-22 11:48:09

标签: sql asp.net

我有一个表格,可以为错误ID分配错误优先级。但有时当您定义错误时,它可能有2个错误优先级。它在db:

中看起来像这样
SELECT g.groupederrorid, f.firederrorpriority
FROM dbo.mon_tbl_groupederrorlog g, dbo.mon_tbl_firederrorlogs f 
WHERE g.groupederrorid = f.groupederrorid
WHERE applicationdbname = 'IS_APP_ELEM_STG'
AND g.errorclosed IS NULL
AND g.errordropped IS NULL
AND g.errorflowcorrected IS NULL


groupederrorid FiredErrorPriority
2411           0
3156           0
3157           0
2411           1

现在在ASP中我有标签来计算高,中等优先级错误的数量。当然,如果errorID具有两个不同的优先级,则它将在两个标签中计算。如果还为低优先级标签分配了中等优先级,如何防止它被计算?

在前端,它看起来像这样 enter image description here

如果errorID已经为其分配了更高的优先级,则不应计算低优先级错误。

以下是用于获取这些数字的SQL:

@"select count(*)
                            from dbo.mon_tbl_groupederrorlog g, dbo.mon_tbl_firederrorlogs f
                            where g.GroupedErrorID = f.GroupedErrorID
                            and ApplicationDBName = @ApplicationDBName
                            and f.firederrorpriority = '1'
                            and g.errorclosed is null
                            and g.errorDropped is null";

中等优先级

 @"select count(*)
                        from dbo.mon_tbl_groupederrorlog g, dbo.mon_tbl_firederrorlogs f
                        where g.GroupedErrorID = f.GroupedErrorID
                        and ApplicationDBName = @ApplicationDBName
                        and f.firederrorpriority = '0'
                        and g.errorclosed is null
                        and g.errorDropped is null";

为低优先级。

1 个答案:

答案 0 :(得分:1)

使用GROUP BYSELECT g.groupederrorid, MAX(f.firederrorpriority) FROM dbo.mon_tbl_groupederrorlog g INNER JOIN dbo.mon_tbl_firederrorlogs f ON g.groupederrorid = f.groupederrorid WHERE applicationdbname = 'IS_APP_ELEM_STG' AND g.errorclosed IS NULL AND g.errordropped IS NULL AND g.errorflowcorrected IS NULL GROUP BY g.groupederrorid 最高

{{1}}