SELECT *, CASE WHEN MAX(salesprice) OVER (PARTITION BY Var, [Group]) <> MIN(salesprice) OVER (PARTITION BY Var, [Group])
THEN 'need to define' ELSE ''
END AS Comment
FROM mytable
ORDER BY ID
如果您使用的是 SQL SERVER 2022+,则使用该 WINDOW 子句定义一次窗口并在查询中多次引用它:
SELECT *, CASE WHEN max(salesprice) OVER win <> MIN(salesprice) OVER win
THEN 'need to define' ELSE ''
END
FROM mytable
WINDOW win AS (PARTITION BY Var, [Group])
ORDER BY ID