Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
kashyap81
Starting Member
9 Posts |
Posted - 2009-02-09 : 00:58:57
|
Lets say we have a table structure such as this:MainTable---------MainTableIDMainTableNameTableA------MainTableIDTableAIDTableANameTableB------MainTableIDTableBIDTableBNameFor every record in MainTable there are 3 corresponding records in TableA and TableB. I want a query that returns the data in this format:MainTableID, MainTableName, TableAID1, TableAName1, TableAID2, TableAName2, TableAID3, TableAName3, TableBID1, TableBName1, TableBID2, TableBName2, TableBID3, TableBName3Is this possible?DiaTree.comD.I.P. Pvt Ltd |
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-09 : 01:10:18
|
Post ur sample data for tables,and Use dynamic cross tab |
|
|
kashyap81
Starting Member
9 Posts |
Posted - 2009-02-09 : 02:05:37
|
Ok... here's some sample data.MainTable---------MainTableID MainTableName1 Joe2 JackTableA------TableAID MainTableID TableAName1 1 Football2 1 Baseball3 1 Basketball4 2 Tennis5 2 Cricket6 2 SoccerTableB------TableBID MainTableID TableBName1 1 Pasta2 1 Pizza3 1 Burger4 2 Noodles5 2 Burrito6 2 SandwichI want the data to be displayed as follows:MainTableID MainTableName TableAName1 TableAName2 TableAName3 TableBName1 TableBName2 TableBName31 Joe Football Baseball Basketball Pasta Pizza Burgers2 Jack Tennis Cricket Soccer Noodles Burrito SandwichDiaTree.comD.I.P. Pvt Ltd |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-09 : 03:55:05
|
[code]SELECT m.MainTableName,MAX(CASE WHEN a.Seq=1 THEN a.TableAName ELSE NULL END) AS TableAName1,MAX(CASE WHEN a.Seq=1 THEN a.TableAID ELSE NULL END) AS TableAID1,MAX(CASE WHEN a.Seq=2 THEN a.TableAName ELSE NULL END) AS TableAName2,MAX(CASE WHEN a.Seq=2 THEN a.TableAID ELSE NULL END) AS TableAID2,MAX(CASE WHEN a.Seq=3 THEN a.TableAName ELSE NULL END) AS TableAName3,MAX(CASE WHEN a.Seq=3 THEN a.TableAID ELSE NULL END) AS TableAID3,MAX(CASE WHEN b.Seq=1 THEN a.TableBName ELSE NULL END) AS TableBName1,MAX(CASE WHEN b.Seq=1 THEN a.TableBID ELSE NULL END) AS TableBID1,MAX(CASE WHEN b.Seq=2 THEN a.TableBName ELSE NULL END) AS TableBName2,MAX(CASE WHEN b.Seq=2 THEN a.TableBID ELSE NULL END) AS TableBID2,MAX(CASE WHEN b.Seq=3 THEN a.TableBName ELSE NULL END) AS TableAName3,MAX(CASE WHEN b.Seq=3 THEN a.TableBID ELSE NULL END) AS TableBID3FROM MainTable mINNER JOIN (SELECT (SELECT COUNT(*) FROM TableA WHERE MainTableID=t.MainTableID AND TableAID <=t.TableAID) AS Seq,* FROM TableA t)aON a.MainTableID=m.MainTableIDINNER JOIN (SELECT (SELECT COUNT(*) FROM TableB WHERE MainTableID=t.MainTableID AND TableBID <=t.TableBID) AS Seq,* FROM TableB t)bON b.MainTableID=m.MainTableIDGROUP BY m.MainTableName[/code] |
|
|
kashyap81
Starting Member
9 Posts |
Posted - 2009-02-09 : 05:41:02
|
I tried this and it worked! Thank you very much Visakh.DiaTree.comD.I.P. Pvt Ltd |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-09 : 06:15:31
|
welcome |
|
|
|
|
|
|
|