2013-03-20 115 views
1

我有一個SQL我試圖在DatasetAdapter中創建它工作到某個點,但我無法弄清楚最後一點工作。在SELECT語句中計算錯誤

我有一個表(MainTable)具有以下字段:ID名稱圖像區域平均

我想計算ABS(Area-?) AS Cal1, ABS(Mean-?) AS Cal2 , Cal1+Cal2 As Cal3

SELECT 
ID, Image, Name, Area, Mean, ABS(Area - ?) AS cal1, ABS(Mean - ?) AS cal2, Cal1+Cal2 AS cal3 
FROM MainTable 

我於是想CAL3的最大值

SELECT  MAX(cal3) AS maxofcal3 
FROM   
    (SELECT  ID, Image, Name, Area, Mean, ABS(Area - ?) AS cal1, ABS(Mean - ?) AS cal2, derivedtbl_1.cal1 + derivedtbl_1.cal2 AS cal3 
FROM   MainTable) 
derivedtbl_1 

它迄今爲止工作給我cal3的最大值。

問題: 我想進行另一個計算(Cal3/maxofcal3)。我無法弄清楚。我想下面的代碼,但想出了一個錯誤:

SELECT  MAX(Cal3) AS maxofCal3, Cal3/MaxofCal3 AS Norm 
FROM   (SELECT  ID, Image, Name, Area, Mean, ABS(Area - ?) AS cal1, ABS(Mean - ?) AS cal2, derivedtbl_1.cal1 + derivedtbl_1.cal2 AS cal3 
                derivedtbl_1.Cal1 + derivedtbl_1.Cal2 AS Cal3 
          FROM   MainTable) derivedtbl_1 
GROUP BY Cal3/MaxofCal3. 
+0

錯誤是什麼? – 2013-03-28 01:11:37

回答

1

集團通過CAL4只,而不是由CAL4/MaxofCal4

檢查下面的例子中得到的想法如何使你的代碼工作:

CREATE TABLE #SampleTable (ID int, Image nvarchar(10), Name nvarchar(10), Area int, Mean int) 


SELECT MAX(Cal3) AS maxofCal3, Cal4/MAX(Cal3) AS Norm 
FROM (
SELECT ID, Image, Name, Area, Mean, ABS(Area - 1) AS cal1, ABS(Mean - 1) AS cal2, ABS(Area - 1) + ABS(Area - 1) AS cal3, ABS(Area - 1) + ABS(Area - 1) AS Cal4 FROM #SampleTable 
) derivedtbl_1 
GROUP BY Cal4 


DROP TABLE #SampleTable