CREATE VIEW View_Fault
As
SELECT a.AppMonth, a.LineID, SUM(OffLines) OffLineSum, SUM(DepDelay + ArrDelay) NormalSum, SUM(SkipLine) SkipLineSum,
SUM(Clear) ClearSum,
(SELECT COUNT(1)
FROM RunFault B
WHERE (DelayTypeID = 2) AND (LineID = A.LineID) AND (to_char(faultdate,'yyyyMM') = AppMonth)) DelayFiveSum
FROM View_FaultALL A
WHERE (nvl(IsDelete, 0) = 0) AND (Status = 1)
GROUP BY a.AppMonth, a.LineID
一直报错:去掉DelayFiveSum那一段就好了,难道DelayFiveSum这个子查询结果不被group by识别为聚合函数?我该怎么解决呢?
根据你的SQL猜了下应该是ORACLE,得SQL如下
CREATE VIEW View_Fault AS SELECT X.*, ( SELECT COUNT(1) FROM RunFault B WHERE B.DelayTypeID = 2 AND B.LineID = X.LineID AND to_char(B.faultdate, 'yyyyMM') = X.AppMonth ) AS DelayFiveSum FROM ( SELECT A.AppMonth, A.LineID, SUM(OffLines) AS OffLineSum, SUM(DepDelay + ArrDelay) AS NormalSum, SUM(SkipLine) AS SkipLineSum , SUM(Clear) AS ClearSum FROM View_FaultALL A WHERE nvl(IsDelete, 0) = 0 AND Status = 1 GROUP BY A.AppMonth, A.LineID ) X