select * from table1
where
(
(
--group1
amount_1 > 0
or
amount_2 > 0
or
amount_3 > 0
)
or
(
--group2
amount_4 > 0
or
amount_5 > 0
or
amount_6 > 0
)
)
or
amount_7 > 0
;
上面是一个初步的sql语句。我需要添加一个功能,以便在两组或更多组中使用至少三个数量,并且每个组(group1和group2)至少使用一个数量。
示例1:如果第1组的1-3数量小于0,则使用amount_7。
示例2:如果group1的数量_1大于零,而group2的value_5和amount_6大于零,则使用amount_1,amount_5,amount_6。
我应该使用一些案例结构,当数量大于0或如何时给出1和0?
在此示例中,金额是预先计算的销售额,不再需要总计。
答案就是这样,仍在测试中。...
select * from table1
where
(
(
--group1
case when
(case when amount_1 > 0 then 1 else 0 end)
+
(case when amount_2 > 0 then 1 else 0 end)
+
(case when amount_3 > 0 then 1 else 0 end)
> 0
then
(
--group1
amount_1 > 0
or
amount_2 > 0
or
amount_3 > 0
)
else null end
)
or
(
--group2
(case when amount_4 > 0 then 1 else 0 end)
+
(case when amount_5 > 0 then 1 else 0 end)
+
(case when amount_6 > 0 then 1 else 0 end)
> 0
then
(
--group2
amount_4 > 0
or
amount_5 > 0
or
amount_6 > 0
)
else null end
)
)
or
amount_7 > 0
;
或更简单的解决方案:
select * from table1
where
(
(
--group1
amount_1
+
amount_2
+
amount_3
>0
)
and
(
--group2
amount_4
+
amount_5
+
amount_6
>0
)
)
or
amount_7 > 0
;