手順が進みsql server 2008 r2
ます(知識がなく、苦手です。)MYSQL
--EXEC ABC
CREATE PROCEDURE ABC
AS
BEGIN
BEGIN TRY
if OBJECT_ID('tempdb..#app') is not null
begin
drop table #app
end
create table #app
(
AppNo int,
Location varchar(10),
Department varchar(10),
Code int
)
insert into #app
values
(1 ,'NY' ,'XYZ1' , 1),
(1 ,'NY' ,'ABC1' , 2),
(1 ,'NY' ,'PQR1' , 3),
(2 ,'TX' ,'XYZ' , 1),
(2 ,'TX' ,'ABC' , 2)
--select * from #app
if OBJECT_ID('tempdb..#t') is not null
begin
drop table #t
end
create table #t
(
AppNo int,
Location1 varchar(10),
Department1 varchar(10),
Code1 int,
Location2 varchar(10),
Department2 varchar(10),
Code2 int,
Location3 varchar(10),
Department3 varchar(10),
Code3 int,
Location4 varchar(10),
Department4 varchar(10),
Code4 int,
Location5 varchar(10),
Department5 varchar(10),
Code5 int,
)
insert into #t
(
AppNo,
Location1,
Department1,
Code1,
Location2,
Department2,
Code2,
Location3,
Department3,
Code3,
Location4,
Department4,
Code4,
Location5,
Department5,
Code5
)
select
DISTINCT
A.AppNo AS AppNo,
A1.Location AS Location1,
A1.Department AS Department1,
A1.Code AS Code1,
A2.Location AS Location1,
A2.Department AS Department1,
A2.Code AS Code1,
A3.Location AS Location1,
A3.Department AS Department1,
A3.Code AS Code1,
A4.Location AS Location1,
A4.Department AS Department1,
A4.Code AS Code1,
A5.Location AS Location1,
A5.Department AS Department1,
A5.Code AS Code1
from #app A
INNER JOIN #app A1 ON A.AppNo=A1.AppNo AND A1.Code=1
LEFT JOIN #app A2 ON A.AppNo=A2.AppNo AND A2.Code=2
LEFT JOIN #app A3 ON A.AppNo=A3.AppNo AND A3.Code=3
LEFT JOIN #app A4 ON A.AppNo=A4.AppNo AND A4.Code=4
LEFT JOIN #app A5 ON A.AppNo=A5.AppNo AND A5.Code=5
SELECT * FROM #t
END TRY
BEGIN CATCH
--YOUR ERROR
END CATCH
END