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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 complicated select query

Author  Topic 

ehsanen
Starting Member

2 Posts

Posted - 2013-11-22 : 12:23:30
I have 4 tables and I need to create a view from them. I just mention columns of the tables that are needed here.
tbl_team(teamID, teamName, leagueID)
tbl_league(leagueID, leagueName)
tbl_match(matchID, leaugeID, guestTeamID, hostTeamID)
tbl_page(pageID, matchID1, matchID2, ..., matchID100)

Each match has two teams(guest and host). In each page there is match ID for 100 matches. Now I need to write a view that shows details for all matches in a page.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-11-22 : 14:15:06
Do you actually have a table called [tbl_page] with 101 columns?
Do you want the output to show one row for each match? ie: 100 rows per page?
I would think your [tbl_page] should have 2 columns (pageID, matchID)

Be One with the Optimizer
TG
Go to Top of Page

ehsanen
Starting Member

2 Posts

Posted - 2013-11-22 : 14:43:08
Yes, exactly I want to show 1 row for each match. Thus, it is not possible to change tbl_page this way.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-11-22 : 14:55:59
If tbl_page is changed to (pageid, matchid) with 100 rows per page then the query would be easy. Can you change the table structure?
If not you'll have to transform the table in the query using UNPIVOT.

Here is an example with 2 pages of 3 matches each:

;with tbl_page (pageid, match1, match2, match3)
as
(
select 1, 1, 2, 3 union all
select 2, 4, 5, 6
)

,tbl_team(teamID, teamName, leagueID)
as (
select 1, 'team1', 100 union all
select 2, 'team2', 100 union all
select 3, 'team3', 100 union all
select 4, 'team4', 100 union all
select 5, 'team5', 100 union all
select 6, 'team6', 100 union all
select 7, 'team7', 100 union all
select 8, 'team8', 100 union all
select 9, 'team9', 100 union all
select 10, 'team10', 100 union all
select 11, 'team11', 100 union all
select 12, 'team12', 100
)

,tbl_league(leagueID, leagueName)
as (
select 100, 'League100'
)

,tbl_match(matchID, leagueID, guestTeamID, hostTeamID)
as (
select 1, 100, 1, 7 union all
select 2, 100, 2, 8 union all
select 3, 100, 3, 9 union all
select 4, 100, 4, 10 union all
select 5, 100, 5, 11 union all
select 6, 100, 6, 12
)

select p.pageid
,p.matchid
,tg.teamName as [guestTeam]
,th.teamName as [hostTeam]
,l.leagueName
from tbl_league l
inner join tbl_match m on m.leagueID = l.leagueID
inner join tbl_team tg on tg.teamid = m.guestTeamID
inner join tbl_team th on th.teamid = m.hostTeamID
inner join ( --unpivot the matches by page
select u.pageid
,u.matchID
from tbl_page
unpivot (matchID for match in ([match1], [match2], [match3])) as u

) p on p.matchid = m.matchid


OUTPUT:
pageid matchid guestTeam hostTeam leagueName
----------- ----------- --------- -------- ----------
1 1 team1 team7 League100
1 2 team2 team8 League100
1 3 team3 team9 League100
2 4 team4 team10 League100
2 5 team5 team11 League100
2 6 team6 team12 League100


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -