I have a table called as matrix which contains two columns namely cola
and colb
as
shown below:
Table: matrix
create table matrix
(
cola varchar(10),
colb varchar(10)
);
Insertion of rows:
insert into matrix values('A1','B1'),('A2','B2'),('A3','B3'),('A4','B4'),
('A5','B5'),('A6','B6'),('A7','B7'),('A8','B8'),
('A9','B9'),('A10','B10'),('A11','B11'),('A12','B12'),
('A13','B13'),('A14','B14'),('A15','B15'),('A16','B16'),
('A17','B17'),('A18','B18'),('A19','B19'),('A20','B20'),
('A21','B21'),('A22','B22'),('A23','B23'),('A24','B24'),
('A25','B25'),('A26','B26'),('A27','B27'),('A28','B28'),
('A29','B29'),('A30','B30');
Note: I want to show the result in the form of matrix and count which columns belongs to each others and assign the values in the matrix for each column. I have just added 30 records for just example but there may be thousands of records also. So I need to prepare a dynamic pivot table for that. The expected result as shown below.
Expected Result:
A1 A2 A3 A4 A5 A6 ................ A30
------------------------------------------------------------------
B1 | 1 0 0 0 0 0 0
|
B2 | 0 1 0 0 0 0 0
|
B3 | 0 0 1 0 0 0 0
|
B4 | 0 0 0 1 0 0 0
|
B5 | 0 0 0 0 1 0 0
|
B6 | 0 0 0 0 0 1 0
. |
. |
. |
. |
B30| 0 0 0 0 0 0 1