sql server 2008 - I need help in sql query to get this o/p -


i have table structure follows..

enter image description here

and here sample data...

tblteam ---------------------------------- name                                      teamid royal challengers bangalore                 1 chennai super kings                         2 delhi daredevils                            3 sunrisers hyderabad                         4 kolkata knight riders                       5 mumbai indians                              6 kings xi punjab                             7 rajasthan royals                            8 deccan chargers                             9 kochi tuskers kerala                       10 pune warriors                              11 ------------------------------------------------  tblschedule ------------------------------------------------ scheduleid  datetime    team_1  team_2  venuid  1   4/18/08 8:00 pm 1   5   6 2   4/19/08 5:00 pm 2   7   9 3   4/19/08 8:30 pm 3   8   4 4   4/20/08 4:30 pm 5   9   1 5   4/20/08 8:00 pm 1   6   5 6   4/21/08 8:00 pm 8   7   27 7   4/22/08 8:00 pm 3   9   10 8   4/23/08 8:00 pm 2   6   2 9   4/24/08 8:00 pm 8   9   10 10  4/25/08 8:00 pm 6   7   9 11  4/26/08 4:00 pm 5   2   2 12  4/26/08 8:00 pm 1   8   6 ----------------------------------------------- 

the yellow key in pic denote primary key , blue 1 foreign key.

and requirement this....

datetime                 team-1                 team-2         apr 8, 2015 8:00:00 pm   kolkata knight riders  mumbai indians 

please o/p...

join tblteam twice different alias names (t1 & t2):

select scheduleid,datetime,t1.name [team-1],t2.name [team-2] tblschedule s join      tblteam t1 on s.team_1=t1.teamid join       tblteam t2 on s.team_2=t2.teamid order s.scheduleid 

sample result:

scheduleid  datetime                team-1                         team-2 ---------------------------------------------------------------------------------------- 1           april, 18 2008 20:00:00 royal challengers bangalore    kolkata knight riders 2           april, 19 2008 17:00:00 chennai super kings            kings xi punjab 3           april, 19 2008 20:30:00 delhi daredevils               rajasthan royals 4           april, 20 2008 16:30:00 kolkata knight riders          deccan chargers 5           april, 20 2008 20:00:00 royal challengers bangalore    mumbai indians 

sample result in sql fiddle


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -