一个业务部门偷懒产生的sql
一个表里有2条这样的数据,现在要查询出stat_month、jh、bq、date取第二条里的数据,新增一个lj取第一条和第二条的bq之和。
|
stat_month |
branch_no |
branch_name |
jh |
bq |
date |
|
202601 |
420100 |
北京 |
257 |
221 |
20260130 |
|
202602 |
420100 |
北京 |
258 |
66 |
20260201 |
select branch_no,branch_name, sum(case when stat_month='202602' then jh else 0 end) jh, sum(csae when stat_month='202602' then bq else 0 end) bq, sum(bq) lj, (select stat_date from a where a.stat_month='202602' and a.branch_no=t.branch_no) date from t group by branch_no,branch_name

浙公网安备 33010602011771号