| Author |
Topic |
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2006-06-07 : 10:56:34
|
| If I write a query some thing like this, it doesnt work in sql server 2000, but works in sql sever 2005.Select top (Select NoOfSelections From @Test Where RowId = @Counter ) AL.Score From AnswerLibrary AL Inner Join @Test Tmp on Tmp.QuestionId = AL.QuestionID Where RowId = @Counter Order By AL.Score DescI get an error message saying incorrect synatax near ( Any body can say whether this kind of query is possible in sql 2000 or not?ThanksKarunakaran |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-07 : 11:14:41
|
declare @rowCount intSelect @rowCount = NoOfSelections From @Test Where RowId = @Counter set rowcount @rowCountSelect AL.Score From AnswerLibrary AL Inner Join @Test Tmp on Tmp.QuestionId = AL.QuestionID Where RowId = @Counter Order By AL.Score Descset rowcount 0Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-07 : 11:17:28
|
It's not. You can do something like this instead...declare @NoOfSelections intSelect @NoOfSelections = NoOfSelections From @Test Where RowId = @Counterset rowcount @NoOfSelectionsSelect AL.Score From AnswerLibrary AL Inner Join @Test Tmp on Tmp.QuestionId = AL.QuestionID Where RowId = @Counter Order By AL.Score Descset rowcount 0 Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-07 : 11:18:20
|
| Doh! Nice work spirit...Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-07 : 11:25:49
|
there a special word we use when this happens it's called sniping  = [s n i p e d] without spacesGo with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2006-06-07 : 11:29:21
|
| Thanks Spirit & Ryan...Karunakaran |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-07 : 11:30:29
|
I was looking for that! I will add it to my favorites ready for next time... Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2006-06-07 : 11:46:37
|
Now I'm getting this error Server: Msg 443, Level 16, State 2, Procedure udfMaxScore, Line 28Invalid use of 'UNKNOWN TOKEN' within a function.Wherever I have used set rowcount, that line gives error.The full code:CREATE FUNCTION dbo.udfMaxScore( -- Add the parameters for the function here @QSetID int,@GroupID int)RETURNS intASBEGIN -- Declare the return variable hereDeclare @Score int, @Counter int,@NoOfQuestions int,@TotalScore int Declare @Test Table (RowId int Identity(1,1), QuestionId int, Score int,NoOfSelections int,GroupId int)Declare @rowcount intSet @Counter = 1Select @NoOfQuestions = count(distinct questionid) from qsetdetail where qsetid = @QSetID--Create Table @Test (RowId int Identity(1,1), QuestionId int, Score int,NoOfSelections int)If @GroupID Is NullBeginInsert into @Test (QuestionId,Score,NoOfSelections)Select distinct QL.QuestionId,Null,NoOfSelections from AnswerLibrary AL Inner Join QuestionLibrary QL on QL.QuestionId = AL.QuestionId Inner Join QsetDetail QD on QD.QuestionId = AL.QuestionIdWhere Qsetid = @QSetIDSelect @NoOfQuestions = count(questionid) from @TestWhile @Counter <= @NoOfQuestionsBeginSelect @rowcount = NoOfSelections From @Test Where RowId = @Counterset rowcount @rowcountUpdate @Test Set Score = (Select Sum(Score) as Score From (Select Top 100 percent AL.Score From AnswerLibrary AL Inner Join @Test Tmp on Tmp.QuestionId = AL.QuestionID Where RowId = @Counter Order By AL.Score Desc)Temp) Where RowId = @Counterset rowcount 0Set @Counter = @Counter+1EndSelect @TotalScore = Sum(Score) from @TestEndRETURN @TotalScoreEND ThanksKarunakaran |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-07 : 11:52:44
|
| You can't do this in a function. I think you should either do it in a stored procedure instead, or you should take a step back and describe what it is you are trying to achieve overall (by supplying DDL, sample data, expected results for that sample data etc).Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2006-06-07 : 12:47:44
|
| [code]declare @ql table(qid int,qdesc varchar(1000),noofselections int)insert into @qlselect 1,'test',3union allselect 2,'test2',2declare @al table(aid int identity(1,1),qid int,answer varchar(200),score int)insert into @alselect 1,'ans1',3union allselect 1,'ans1',1union allselect 1,'ans1',5union allselect 1,'ans1',8union allselect 1,'ans1',4union allselect 2,'ans2',5union allselect 2,'ans2',9union allselect 2,'ans2',1[/code][code]Final output would beqid score1 17 ( 8+5+4) This is the sum top 3 (which is the value of noofselections) score desc2 14 (9+5) This is the sum top 2 (which is the value of noofselections) score desc [/code]ThanksKarunakaran |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-07 : 13:14:35
|
here you go. but it's not preety  declare @ql table(qid int,qdesc varchar(1000),noofselections int)insert into @qlselect 1,'test',3union allselect 2,'test2',2declare @al table(aid int identity(1,1),qid int,answer varchar(200),score int)insert into @alselect 1,'ans1',3union allselect 1,'ans1',1union allselect 1,'ans1',5union allselect 1,'ans1',8union allselect 1,'ans1',4union allselect 2,'ans2',5union allselect 2,'ans2',9union allselect 2,'ans2',1select * from @qlselect * from @alselect t3.qid, sum(t4.score)from @ql t3 join ( select rank = count(*), t1.qid, t1.score from @al t1 join @al t2 on t1.qid + t1.score >= t2.qid + t2.score group by t1.qid, t1.score) t4 on t3.qid = t4.qidwhere t4.rank > t3.noofselectionsgroup by t3.qid Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-07 : 13:15:53
|
Something like this...--datadeclare @ql table (qid int, qdesc varchar(1000), noofselections int)insert into @ql select 1,'test',3union all select 2,'test2',2declare @al table (aid int identity(1,1), qid int, answer varchar(200), score int)insert into @al select 1,'ans1',3union all select 1,'ans1',1union all select 1,'ans1',5union all select 1,'ans1',8union all select 1,'ans1',4union all select 2,'ans2',5union all select 2,'ans2',9union all select 2,'ans2',1--calculationselect a.qid, sum(score) as score from @al a inner join @ql b on a.qid = b.qid and b.noofselections >= (select count(*) from @al where qid = a.qid and score >= a.score)group by a.qid/*resultsqid score ----------- ----------- 1 172 14*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2006-06-07 : 14:16:45
|
| Thanks Again.Karunakaran |
 |
|
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2006-06-29 : 11:16:47
|
This query is failing for this set of records, I tried to see whats going wrong. I couldnt find anything . Otherwise it works fine.declare @al table (aid int, qid int, score int)insert into @alSelect 301,1,105 union allSelect 302,0,105 union allSelect 303,0,106 union allSelect 304,0,106 union allSelect 305,1,106 union allSelect 306,0,106 union allSelect 307,1,107 union allSelect 308,1,107 union allSelect 309,1,107 union allSelect 310,1,107 union allSelect 311,1,107 union allSelect 312,1,107 union allSelect 313,1,107 union allSelect 314,1,107 union allSelect 315,0,107 union allSelect 316,0,108 union allSelect 317,0,108 union allSelect 318,0,108 union allSelect 319,0,108 union allSelect 320,0,108 union allSelect 321,0,108 union allSelect 322,0,108 union allSelect 323,0,108 union allSelect 324,0,108 union allSelect 325,0,108 union allSelect 326,1,109 union allSelect 327,1,109 union allSelect 328,1,109 union allSelect 329,1,109 union allSelect 330,1,109 union allSelect 331,1,109 union allSelect 332,1,109declare @ql table (qid int,noofselections int)insert into @qlSelect 105,1 union allSelect 106,1 union allSelect 107,1 union allSelect 109,1 union allSelect 108,4select a.qid, sum(score) as score from @al a inner join @ql b on a.qid = b.qid and b.noofselections >= (select count(*) from @al where qid = a.qid and score >= a.score)group by a.qidExpected Result is:qid score105 1106 1107 1108 0109 1 ThanksKarunakaranKarunakaran |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-29 : 11:57:28
|
The 1st issue is that the data is the wrong way round in @al. Once that's sorted, there is still a problem, though: It seems the code does not cope with ties. This is tricky at first thought, but let's see what people come up with (I'll have a go myself)...Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2006-06-29 : 12:01:51
|
quote: Originally posted by RyanRandall The 1st issue is that the data is the wrong way round in @al. Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part.
Oops.... Here is the revised one for @aldeclare @al table (aid int, score int, qid int)insert into @alSelect 301,1,105 union allSelect 302,0,105 union allSelect 303,0,106 union allSelect 304,0,106 union allSelect 305,1,106 union allSelect 306,0,106 union allSelect 307,1,107 union allSelect 308,1,107 union allSelect 309,1,107 union allSelect 310,1,107 union allSelect 311,1,107 union allSelect 312,1,107 union allSelect 313,1,107 union allSelect 314,1,107 union allSelect 315,0,107 union allSelect 316,0,108 union allSelect 317,0,108 union allSelect 318,0,108 union allSelect 319,0,108 union allSelect 320,0,108 union allSelect 321,0,108 union allSelect 322,0,108 union allSelect 323,0,108 union allSelect 324,0,108 union allSelect 325,0,108 union allSelect 326,1,109 union allSelect 327,1,109 union allSelect 328,1,109 union allSelect 329,1,109 union allSelect 330,1,109 union allSelect 331,1,109 union allSelect 332,1,109 Karunakaran |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-29 : 12:43:52
|
Karunakaran - Here's my revised effort...  select a.qid, sum(score) as score from @ql a left outer join @al b on a.qid = b.qid and a.noofselections >= (select count(*) from @al where qid = b.qid and (score > b.score or (score = b.score and aid >= b.aid)))group by a.qid Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2006-06-29 : 13:29:17
|
That works great...Thanks Ryan Karunakaran |
 |
|
|
|
|
|