I am looking for someone to point me in the right direction how to traverse result set and insert into table without using cursor. Not even sure how to ask this.
Result set of the select query:
------------------------------------------------------
ID | Name | CreatedBy | CreatedDate
------------------------------------------------------
1 A John 2018-04-30
1 B Sam 2018-04-20
2 A John 2018-04-18
-------------------------------------------------------
Inserted table:
---------------------------------------------------------------------------------------------------------
ID | A_CreatedBy | A_CreatedDate | B_CreatedBy | B_CreatedDate | C_CreatedBy | C_CreatedDate
---------------------------------------------------------------------------------------------------------
1 John 2018-04-30 Sam 2018-04-20 NULL NULL
2 John 2018-04-18 NULL NULL NULL NULL
---------------------------------------------------------------------------------------------------------
Any help would be appreciated.
You can use the conditional aggregation as follows:
select ID,
max(case when name = 'A' then createdby end) as a_createdby,
max(case when name = 'A' then CreatedDate end) as a_CreatedDate,
max(case when name = 'B' then createdby end) as b_createdby,
max(case when name = 'B' then CreatedDate end) as b_CreatedDate,
max(case when name = 'C' then createdby end) as c_createdby,
max(case when name = 'C' then CreatedDate end) as c_CreatedDate
from (your_query) t
group by ID
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.