简体   繁体   中英

Adding new rows based on column value in SQL?

Comment         weekid  acc1    acc2    acc3    acc4       value
----------------------------------------------------------------------------------
Current data        1   a      b       c       d             2
Current data        1   a      b       c       e             3
Line to be added    1   a      b       c       Fixed New     value of d/value of e 

Any help will be greatly appreciated

I suspect you just want union all :

select weekid, acc1, acc2, acc3, acc4, value
from t
union all
select weekid, acc1, acc2, acc3, 'Fixed new' as acc4,
       ( max(case when acc4 = 'd' then value end) /
         max(case when acc4 = 'e' then value end)
       ) as value
from t
group by weekid, acc1, acc2, acc3;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM