CrossTab
-
Hello, I have two tables as below Table1 (where id is Pk key) Id Name 1 A 2 B Table2 Id Reasons 1 X 2 Y 1 Z I need output like as below ID NAME REASON1 REASON2 1 A X Z 2 B Y Null Amit
-
Hello, I have two tables as below Table1 (where id is Pk key) Id Name 1 A 2 B Table2 Id Reasons 1 X 2 Y 1 Z I need output like as below ID NAME REASON1 REASON2 1 A X Z 2 B Y Null Amit
If you're using SQL Server 2005, take a look at the Pivot command.
Deja View - the feeling that you've seen this post before.
-
If you're using SQL Server 2005, take a look at the Pivot command.
Deja View - the feeling that you've seen this post before.
Any example please....
-
Hello, I have two tables as below Table1 (where id is Pk key) Id Name 1 A 2 B Table2 Id Reasons 1 X 2 Y 1 Z I need output like as below ID NAME REASON1 REASON2 1 A X Z 2 B Y Null Amit
see the following example : it may help u (run it in sql query analyser) CREATE TABLE #T1(id int,Name varchar(10)) insert into #T1 (id,Name) values (1,'Haris') insert into #T1 (id,Name) values (2,'Arshad') --select * from #T1 CREATE TABLE #T2(id int,Name varchar(5)) insert into #T2 (id,Name) values (1,'X') insert into #T2 (id,Name) values (2,'Y') insert into #T2 (id,Name) values (1,'Z') insert into #T2 (id,Name) values (1,'X') SELECT Id,Emp,X,Y,Z FROM ( SELECT #T1.Id,#T1.Name AS Emp,#T2.Name FROM #T1 INNER JOIN #T2 ON #T2.Id = #T1.Id ) s PIVOT ( COUNT(Name) FOR Name IN (X,Y,Z) ) p drop table #T1 drop table #T2
fasih_is_my_signature
-
see the following example : it may help u (run it in sql query analyser) CREATE TABLE #T1(id int,Name varchar(10)) insert into #T1 (id,Name) values (1,'Haris') insert into #T1 (id,Name) values (2,'Arshad') --select * from #T1 CREATE TABLE #T2(id int,Name varchar(5)) insert into #T2 (id,Name) values (1,'X') insert into #T2 (id,Name) values (2,'Y') insert into #T2 (id,Name) values (1,'Z') insert into #T2 (id,Name) values (1,'X') SELECT Id,Emp,X,Y,Z FROM ( SELECT #T1.Id,#T1.Name AS Emp,#T2.Name FROM #T1 INNER JOIN #T2 ON #T2.Id = #T1.Id ) s PIVOT ( COUNT(Name) FOR Name IN (X,Y,Z) ) p drop table #T1 drop table #T2
fasih_is_my_signature
Thanks but i don't want to count. My rwq is little different and i would really appreciate if you could help me in this.. I have one table CREATE TABLE #T1(id int,Name varchar(10)) insert into #T1 (id,Name) values (1,'Haris') insert into #T1 (id,Name) values (2,'Arshad') CREATE TABLE #T2(id int,Reason varchar(5)) insert into #T2 (id,Reason) values (1,'X') insert into #T2 (id,Reason) values (2,'Y') insert into #T2 (id,Reason) values (1,'Z') insert into #T2 (id,Reason) values (1,'X') Now i need output like Id Name Reason1 Reason2 Reason3 ......ReasonN 1 Haris X Z X 2 Arshad Y NULL NULL Thanks Amit