我有一个我试图在DatasetAdapter
中创建的SQL,它在某一点上起作用,但我不知道最后一点是否正常工作。
我有一个表(MainTable),其中包含以下字段:ID Name Image Area Mean
我想计算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 :(得分: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