我有sql查询,其中我想按特定列返回具有不同值顺序的行。就像说,我要按locationid从订单表中取消batchno。
我也尝试过谷歌,但无法找到解决方案
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
那是我每次尝试所得到的。
当不使用不同的我得到输出,但使用此查询有重复的行
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]
所以这是我尝试过的事情,但是没有运气。请帮助我。
DISTINCT不允许列表中ORDER BY也没有的任何表达式SELECT(即使在这种情况下,该表达式只能为每个不同的行产生一个特定的值)。您可以使用。
DISTINCT
ORDER BY
SELECT
SELECT Batchno, LocationId FROM Ordertbl GROUP BY Batchno, LocationId ORDER BY CASE WHEN[LocationId] = 3 THEN 0 ELSE 1 END, [LocationId]