SQL添加多列

时间:2019-03-14 09:01:33

标签: sql vb.net

我试图做一个前10名,但将多个列加在一起并按一定日期范围内的组件将它们分组,如果我取出SUM,它就像一个符咒,但是当我将它们放回去时,出现以下异常,已经有一段时间了,我可能只是脑袋了。

sql = "SELECT TOP 10 ComponentName, (SUM(ISNULL(ulCompSizeFailure,0)) + SUM(ISNULL(ulLeadCountFailure,0)) + SUM(ISNULL(ulVPSHeightFailure,0))) as Total 
       FROM [i_import_slot_vision_errors] 
       WHERE DATE_TIME >= @startdata2 
           AND DATE_TIME < @enddata2 
       GROUP BY ComponentName 
       ORDER BY (SUM(ISNULL(ulCompSizeFailure,0)) + SUM(ISNULL(ulLeadCountFailure,0)) + SUM(ISNULL(ulVPSHeightFailure,0))) as Total DESC"

问题是我收到了SQL异常,“关键字'as'附近的语法不正确。”

1 个答案:

答案 0 :(得分:2)

这是一种正确的语法,您不能为ORDER BY子句加上别名。

SELECT TOP 10 ComponentName, (SUM(ISNULL(ulCompSizeFailure,0)) + SUM(ISNULL(ulLeadCountFailure,0)) + SUM(ISNULL(ulVPSHeightFailure,0))) as Total 
FROM [i_import_slot_vision_errors] 
WHERE DATE_TIME >= @startdata2 AND DATE_TIME < @enddata2 
GROUP BY ComponentName 
ORDER BY Total DESC

别忘了,在SQL中ORDER BY之后,SELECT子句被计算机读取,您可以在SELECT中使用ORDER BY中的别名。