SQL CE:Select 不同,按另一列排序
SQL CE: Select distinct, order by another column
我想知道如何对我得到的数据进行排序,我想做一个 select distinct(name) 并按 ID 排序,如下所示:
var querythis = "SELECT distinct(exerVariName) as variants FROM Test WHERE date = @0 ORDER BY ID";
所以我也必须 select distinct(id),这可能实现吗?
使用 DISTINCT 时,您不能按不在 select 列表中的列排序。
但是您也可以使用 GROUP BY 获得不同的结果:
SELECT exerVariName as variants
FROM Test
WHERE date = @0
GROUP BY exerVariName
ORDER BY MIN(ID) -- or MAX(ID)
编辑:
如果 CE 不允许在 ORDER BY 中使用聚合函数,您可以尝试 Derived Table(希望它被允许):
SELECT variants
FROM
(
SELECT exerVariName as variants, MIN(ID) as minID -- or MAX(ID)
FROM Test
WHERE date = @0
GROUP BY exerVariName
) AS dt
ORDER BY minID
试试这个:
select exerVariName as variants,
MIN(ID) as min_id
from Test
where date = @0
group by exerVariName
order by min_id
我想知道如何对我得到的数据进行排序,我想做一个 select distinct(name) 并按 ID 排序,如下所示:
var querythis = "SELECT distinct(exerVariName) as variants FROM Test WHERE date = @0 ORDER BY ID";
所以我也必须 select distinct(id),这可能实现吗?
使用 DISTINCT 时,您不能按不在 select 列表中的列排序。
但是您也可以使用 GROUP BY 获得不同的结果:
SELECT exerVariName as variants
FROM Test
WHERE date = @0
GROUP BY exerVariName
ORDER BY MIN(ID) -- or MAX(ID)
编辑:
如果 CE 不允许在 ORDER BY 中使用聚合函数,您可以尝试 Derived Table(希望它被允许):
SELECT variants
FROM
(
SELECT exerVariName as variants, MIN(ID) as minID -- or MAX(ID)
FROM Test
WHERE date = @0
GROUP BY exerVariName
) AS dt
ORDER BY minID
试试这个:
select exerVariName as variants,
MIN(ID) as min_id
from Test
where date = @0
group by exerVariName
order by min_id