?
select t.客戶姓名,sum(case when t.收款類型='首款' then t.金額 else 0 end as '首款'),sum(case when t.收款類型='尾款' then t.金額 else 0 end as '尾款') from table t group by t.客戶姓名
這段sql的意思 是 查詢出所有客戶收款信息 然后按客戶分組 ? ?分組后 ?然后將這個客戶的所有首款的金額放在一組 然后聚合 組成首款字段 ? 所有尾款金額 放在一組 然后sum 聚合 組成尾款字段
最終查出?
統計的時候 或者 做運算的時候非常有用
?
?
?
如果需要列轉行 可以使用union方式 比如我們上面這個表
select t.客戶姓名,t.首款 from table t where type='首款' union select t.客戶姓名,t.尾款 from table t where type='尾款'
?