Author |
Topic |
lstephan
Starting Member
12 Posts |
Posted - 2011-09-30 : 12:09:53
|
Hi,I ve made a table league for football (ot works fine) but now i would like to select my favorite team and juste the 2 teams before and after my favorite one.My SQL query :SELECT masomme, equipe_id, name, logo_small, diff, win, equal, lost, joues, ROW_NUMBER() OVER (ORDER BY masomme DESC, diff DESC, win DESC, lost ASC) AS RowNumberFROM lstephan.classementgeneralSo i can choose my favorite team by (where equipe_id =4) but i would like to choose 2 before and 2 after...Any idea?Thanks a lot. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-30 : 12:14:18
|
[code]DECLARE @FavTeamID intSET @FavTeamID=4SELECT *FROM(SELECT masomme, equipe_id, name, logo_small, diff, win, equal, lost, joues, DENSE_RANK() OVER (ORDER BY (equipe_id-@FavTeamID) ASC) AS rnFROM lstephan.classementgeneral)tWHERE rn <=3[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
lstephan
Starting Member
12 Posts |
Posted - 2011-09-30 : 12:28:48
|
HiThanks a lot visakh16 for your responseBut i probably don't explain my problem as well...My ranking give me all teams, their points and position (rank).Like thisrank team points idteam1 team xy 30 52 team abc 29 13 team qsd 25 88910 myteam 10 41112So i want just to display myteam and 2 teams before and afterIs it more clear?Thanks a lot |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-30 : 12:33:55
|
[code]declare @teamname varchar(100)set @teamname='myteam';With cteas(select *from tablewhere team = @teamnameunion allselect t.*from table tjoin cte con abs(c.rank-t.rank) = 2)select * from cte[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
lstephan
Starting Member
12 Posts |
Posted - 2011-09-30 : 12:40:19
|
Thanks a lot but sorry i dont really understand |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-30 : 12:48:29
|
did you try it first?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
lstephan
Starting Member
12 Posts |
Posted - 2011-09-30 : 12:51:32
|
Yes i tried it.My query for generating rank :SELECT masomme, equipe_id, name, logo_small, diff, win, equal, lost, joues, ROW_NUMBER() OVER (ORDER BY masomme DESC, diff DESC, win DESC, lost ASC) AS RowNumberFROM lstephan.classementgeneralAnd i m a poor sql developper lol |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-30 : 13:18:19
|
[code]declare @teamname varchar(100)set @teamname='myteam'SELECT masomme, equipe_id, name, logo_small, diff, win, equal, lost, joues, ROW_NUMBER() OVER (ORDER BY masomme DESC, diff DESC, win DESC, lost ASC) AS RowNumber INTO #tempFROM lstephan.classementgeneral;With CTEAS(SELECT * FROM #TempWHERE name= @nameUNION ALLSELECT t.*FROM #Temp tINNER JOIN CTE cON ABS(c.RowNumber-t.RowNumber)<=2AND c.c.RowNumber<>t.RowNumber)SELECT *FROM CTE[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
lstephan
Starting Member
12 Posts |
Posted - 2011-09-30 : 14:30:26
|
Sorry i must be so stupid lolBut when i tried to execute it , i ve got error.I really dont understand why using some "variable" :declare @teamname varchar(100)set @teamname='myteam'SELECT masomme, equipe_id, name, logo_small, diff, win, equal, lost, joues, ROW_NUMBER() OVER (ORDER BY masomme DESC, diff DESC, win DESC, lost ASC) AS RowNumber INTO #tempFROM lstephan.classementgeneral (here ok);With CTE AS(SELECT * FROM #TempWHERE name= @nameUNION ALLSELECT t.*FROM #Temp tINNER JOIN CTE cON ABS(c.RowNumber-t.RowNumber)<=2AND c.c.RowNumber<>t.RowNumber)SELECT *FROM CTEI dont understand (bold expressions)But dont want you to spend too much time on it... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-01 : 14:03:46
|
its place where you check for 2 records before and after your rank. abs(difference) =2 will give same record as well as 2 before and 2 after. c.c.RowNumber<>t.RowNumber is to exclude same record from coming again------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
lstephan
Starting Member
12 Posts |
Posted - 2011-10-02 : 03:17:19
|
Thanks a lot i will try it.Have a nice day |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-02 : 03:35:26
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
lstephan
Starting Member
12 Posts |
Posted - 2011-10-02 : 04:06:35
|
Hi,I didnt try it yet but just some explicationWhat is CTE, C and T for?Is it referring to tables?Sorry but i m just trying to understand but i don t want to disturb you anymore. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-02 : 04:12:37
|
CTE is name i gave for common table expression. you can give any name insteadc and t are aliases ie short names------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
lstephan
Starting Member
12 Posts |
Posted - 2011-10-02 : 04:29:32
|
Ok fine thanks a lot i will work on it |
|
|
lstephan
Starting Member
12 Posts |
Posted - 2011-10-02 : 05:50:24
|
I ve tried this :declare @teamname varchar(100)set @teamname='stade brestois'SELECT masomme, equipe_id, name, logo_small, diff, win, equal, lost, joues, ROW_NUMBER() OVER (ORDER BY masomme DESC, diff DESC, win DESC, lost ASC) AS RowNumber INTO #tempFROM lstephan.classementgeneral ;With CTEAS(SELECT * FROM #TempWHERE name=@teamnameUNION ALLSELECT t.*FROM #Temp tINNER JOIN CTE cON ABS(c.RowNumber-t.RowNumber)<=2AND c.RowNumber<>t.RowNumber)SELECT *FROM CTE It selected the right rows but returns me 252 rows i dont know whyThanks a lot. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-02 : 07:44:35
|
will your RowNumber repeat?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
lstephan
Starting Member
12 Posts |
Posted - 2011-10-02 : 10:46:35
|
Hi yes it did.But i could just do select top 4 no? |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-10-02 : 10:49:29
|
[code]ROW_NUMBER() OVER (ORDER BY masomme DESC, diff DESC, win DESC, lost ASC) AS RowNumber[/code]How is it possible for the above RowNumber to have repeating value ? KH[spoiler]Time is always against us[/spoiler] |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-02 : 13:39:55
|
thats what i also couldnt make out------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
lstephan
Starting Member
12 Posts |
Posted - 2011-10-03 : 02:08:39
|
Hi,I will test it again today.Thanks a lot. |
|
|
Next Page
|