select
sum(case when Apt.productionValue != '-1.0' then Apt.subTotal
else Apt.productionValue end) as ProductionValue,Apt.date
from Appointment Apt
group by Apt.date
order by Apt.date asc
apppointmentd | 产值 | 小计 | 日期 | 1 | 1 | 10 | 2021-09-02 | 2
---|---|---|---|
10 | 0 | 2021-09-02 | |
3 | 1 | 20 | 2021-09-01 |
1 | 20 | 2021-09-01 | |
5 | 0 | 2021-09-01 |
你差一点就成功了。这应该基于所提供的样例数据。
select Apt.[Date]
, sum(case when Apt.ProductionValue < 0 then Apt.SubTotal else Apt.ProductionValue end)
from Appointment Apt
group by Apt.[Date]
order by Apt.[Date]
您可以在子查询中选择期望的列,如:
select Date, SUM(ProductionValue) as ProductionValue
from (
select
Date,
case when ProductionValue > 0
then ProductionValue
else SubTotal end
as ProductionValue
from Appointment
) as d
group by Date
order by Date