hello all,
i have a table with below data,
declare @tbl table (uid int, uname varchar(10), start_dt date, end_dt date) insert into @tbl values (1, 'env1', '4/4/2010', '5/5/2012') insert into @tbl values (2, 'env2', '5/4/2010', '6/6/2012') --earlier start data is '4/4/2010' from 'env1' --latest end data is '6/6/2012' from 'env2' insert into @tbl values (3, 'env1', '3/3/2010', '4/4/2012') insert into @tbl values (4, 'env2', '2/2/2010', '5/5/2012') --earlier start data is '2/2/2010' from 'env2' --latest end data is '5/5/2012' from 'env2' insert into @tbl values (5, 'env1', '8/8/2010', '12/12/2012') insert into @tbl values (6, 'env2', '9/9/2010', '10/10/2012') --earlier start data is '8/8/2010' from 'env1' --latest end data is '12/12/2012' from 'env1'
insert into @tbl values (6, 'envX', '9/9/2010', '10/10/2012')
insert into @tbl values (6, 'envY', '9/9/2010', '10/10/2012')
i need to merge 2 rows for column "uname" having value "env1" & "env2" to "envZ" and need to capture earlier start date and latest end date from both and update with new.
the desire output should be,
declare @tbl table (uid int, uname varchar(10), start_dt date, end_dt date) insert into @tbl values (1, 'envZ', '4/4/2010', '6/6/2012') insert into @tbl values (4, 'envZ', '2/2/2010', '5/5/2012') insert into @tbl values (5, 'envZ', '8/8/2010', '12/12/2012') insert into @tbl values (6, 'envX', '9/9/2010', '10/10/2012') insert into @tbl values (6, 'envY', '9/9/2010', '10/10/2012')
note - i must need to update one row and delete other row as i can't insert new rows (having huge data with other columns also).
please suggest optimize query. thanks!