我正在用排名算法編寫一個搜索例程,希望能夠一次通過。是否可以在同一個外部查詢的case語句中使用子查詢的結果?
我的理想查詢會是這樣的....
select *, (select top 1 wordposition
from wordpositions
where recordid=items.pk_itemid and wordid=79588 and nextwordid=64502
) as WordPos,
case when WordPos<11 then 1 else case WordPos<50 then 2 else case WordPos<100 then 3 else 4 end end end end as rank
from items
是否有可能的情況下使用WordPos在那裏?它在我身上產生一個錯誤,無效的列名稱'WordPos'。
我知道我可以重做每個案例的子查詢,但我認爲它會重新運行的情況下不是嗎?
例如:
select *, case when (select top 1 wordposition from wordpositions where recordid=items.pk_itemid and wordid=79588 and nextwordid=64502)<11 then 1 else case (select top 1 wordposition from wordpositions where recordid=items.pk_itemid and wordid=79588 and nextwordid=64502)<50 then 2 else case (select top 1 wordposition from wordpositions where recordid=items.pk_itemid and wordid=79588 and nextwordid=64502)<100 then 3 else 4 end end end end as rank from items
這一工程....但它真的重新運行每次查詢相同?
從測試中很難判斷出它第一次運行很慢,但後續運行很快......它是緩存......所以這意味着它第一次運行它的第一行,後續三次它會從緩存中得到結果?
只是好奇最好的辦法是做什麼... 謝謝! output子句(它來自一個子查詢的事實是無關緊要的)不能同一SELECT語句中使用中介紹 瑞安
對不起T-SQL不是MySQL ...修正了標籤... –