select custom
,sum(case when state=1 then total else 0 end) 应收
,sum(case when state=0 then total else 0 end) 欠款
from table1 group by custom
,sum(case when state=1 then total else 0 end) 应收
,sum(case when state=0 then total else 0 end) 欠款
from table1 group by custom
每个custom有多条记录,有多个custom,现在要统计每个客户的应收款总额和未结算款的总额,用一个sql语句怎么写select custom as 客户姓名
,sum(total) as 应收款
,sum(case state when 1 then 0 else total) as 欠款
from table1
group by custom
你是ACCESS数据库吗?如果是,就用:
select custom as 客户姓名
,sum(total) as 应收款
,sum(iif(state=false,0,total)) as 欠款
from table1
group by custom