【发布时间】:2012-10-09 07:18:33
【问题描述】:
我有 sql 查询,我希望在其中按特定列返回具有不同值顺序的行。 比如说,我想通过 locationid 从 ordertable order 中区分 batchno。
我也试过谷歌,但无法找到解决方案
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
这就是我每次尝试得到的结果。
当不使用 distinct 时,我得到了输出,但使用此查询有重复的行
Select Batchno,LocationId from Ordertbl order by case when[LocationId] =3 THEN 0 ELSE 1 END, [LocationId]
如果我使用 distinct 它会给我错误这些是我尝试过的查询。
Select distinct CAST(Batchno as Varchar(MAX)),LocationId from Ordertbl order by case when[LocationId] =3 THEN 0 ELSE 1 END,[LocationId]
或
Select distinct CAST(Batchno as Varchar(MAX)),LocationId from Ordertbl order by case when[LocationId] =3 THEN 0 ELSE 1 END,[LocationId],[Batchno]
所以这是我尝试过但没有运气的事情。请帮助我。
【问题讨论】:
-
Batchno的数据类型是什么?你为什么要把它投射到Varchar(MAX)?
标签: sql database sql-order-by distinct