我有以下查詢,當前返回2行(它總是隻返回2行)。SQL將同一表中的多個行組合到一行中,並將其填入不同的字段
Select
User_Profile.userid, User_Profile.displayname, User_Profile.picPath,
Battle.id as battleid, Battle.challenger_id as challengerid,
Video.[filename]
From
Battle
INNER Join
User_Profile ON Battle.challenger_id = User_Profile.userid OR
Battle.challenged_id = User_Profile.userid
INNER Join
Video ON User_Profile.userid = Video.[user_id]
WHERE
Video.battle_id = Battle.id
這將返回是這樣的:
userid | displayname | picPath | battleid | challengerid | filename
--------------------------------------------------------------------
6 | CandyPoo | test.jpg | 12 | 9 | test.mp4
9 | Nawlrus | test2.jpg | 12 | 9 | test2.mp4
我期待回到這樣的事情:
battleid | challengerid | xuserid | xdisplayname | xpicPath | xfilename | Yuserid | YdisplayName | YpicPath | YfileName
--------------------------------------------------------------------------------------------------------------------------
12 | 9 | 9 | Nawlrus | test2.jpg | test2.mp4 | 6 | CandyPoo | test.jpg | test.mp4
有沒有辦法做到這一點?我會像上面那樣返回多行(Top X),但具有相同Battleid的行需要像上面看到的表一樣合併。這在SQL Server 2008中可能嗎?