我不明白为什么这不起作用:SQL自定义排序
select distinct a.QuestionID,a.QuestionName,b.AnswerID,b.AnswerName
from @TempExportList a
join tblAnswers b
on a.QuestionID = b.QuestionID
where [email protected]
order by (case when a.QuestionName='A' then 0
when a.QuestionName='B' then 1
else a.QuestionID
end)
我碰到下面的错误 -
ORDER BY项目必须出现在 选择列表中,如果SELECT DISTINCT是 指定的。
但是这工作得很好:
select distinct a.QuestionID,a.QuestionName,b.AnswerID,b.AnswerName
from @TempExportList a
join tblAnswers b
on a.QuestionID = b.QuestionID
where [email protected]
order by a.QuestionID
谢谢。我原以为它引用了a.QuestionName和a.QuestionID不在select中,其中0,1 ... n只是一个订单号,不需要作为select的一部分。 – Bob 2010-12-02 13:23:23
谢谢。错误很明显,但我认为只有列必须添加。对我来说,SQL可以非常简单,同时也很混乱-_- – ErTR 2015-01-29 18:23:32