我尝试创建选择查询但仍在苦苦挣扎。我的目标是从colum AMOUNT接收总金额,但是对于每一行COLUMN_ID,必须至少有一个值小于50并且至少有一个值超过50.总金额必须通过汇总每个值小于50的值来计算,这个总金额必须小于100。
我在下面创建的选择查询只需要添加:每个值小于50的值的总和(金额)
SELECT COLUMN_ID, SUM(AMOUNT)
FROM TABLE1
WHERE ...
GROUP BY COLUMN_ID
HAVING MIN(AMOUNT) < 50 AND MAX(AMOUNT) > 50 AND SUM(AMOUNT) < 100;
答案 0 :(得分:0)
SELECT COLUMN_ID
, SUM(case when ammount < 50 then ammount else 0 end ) sumsmall
FROM T
WHERE ...
GROUP BY COLUMN_ID
HAVING SUM(case when ammount < 50 then ammount else 0 end ) > 0
and SUM(case when ammount > 50 then ammount else 0 end ) > 0
and SUM(case when ammount < 50 then ammount else 0 end ) < 100