我有这个数据
Id Campo
----------- ----------
1 id_recibo
2 col_2
3 col_65
4 UNDCAP
5 id_recibo
6 col_2
7 col_65
8 UNDCAP
我需要像SELECT DISTINCT
那样按照外观顺序获取数据,而不是默认按升序获取数据。
如果我做了SELECT DISTINCT
我就明白了:
campo
--------
col_2
col_65
id_recibo
UNDCAP
这里是代码:
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL
DROP TABLE #tmp
CREATE TABLE #tmp
(
Id INT IDENTITY,
Campo VARCHAR(MAX)
)
INSERT INTO #tmp (Campo)
VALUES ('id_recibo'), ('col_2'), ('col_65'), ('UNDCAP'),
('id_recibo'), ('col_2'), ('col_65'), ('UNDCAP')
SELECT * FROM #tmp
SELECT DISTINCT campo FROM #tmp
我需要的结果是这样的:
campo
----------
id_recibo
col_2
col_65
UNDCAP
我试过这样的一些,但它没有用,因为订单是可变的:
SELECT
A.Campo AS NuevoOrden
FROM
(SELECT DISTINCT
CAMPO
FROM
#tmp) A
ORDER BY NEWID()
答案 0 :(得分:2)
我想你只是想:
select campo
from t
group by campo
order by min(id);