I have the table called as sales with three columns SalesCountry
, SalesState
and SalesMan
as shown below:
Table:
create table sales
(
SalesCountry varchar(20),
SalesState varchar(20),
SalesMan varchar(20)
);
Insertion:
insert into sales values('USA','TEXAS','Mak');
insert into sales values('USA','California','Sam');
insert into sales values('Cannada','Alberta','John');
insert into sales values('Cannada','Manitoba','John');
Now I want to show the result as follows:
County State1 State2 SalesMan1 SalesMan2
---------------------------------------------------------
USA TEXAS California Mak Sam
Cannada Alberta Manitoba John NULL
Note: The pivot query should be dynamic, which should show the columns as per the rows in the table with increment of value 1 with column name like i have shown State1,State2
there may be State3,State4,,,,,,n
. In my example there are only four rows, there may be more than that also.