使用ORDER BY子句替换结果

时间:2011-08-26 16:36:45

标签: sql postgresql

CREATE TABLE TEST (
  customername varchar(50)
);

INSERT INTO TEST VALUES('CUSTOMER 1');
INSERT INTO TEST VALUES('CUSTOMER 1');
INSERT INTO TEST VALUES('CUSTOMER 1');
INSERT INTO TEST VALUES('CUSTOMER 2');
INSERT INTO TEST VALUES('CUSTOMER 2');
INSERT INTO TEST VALUES('CUSTOMER 2');

SELECT * FROM TEST ORDER BY ???

我应该使用什么ORDER BY子句来获得以下结果?:

"CUSTOMER 1"
"CUSTOMER 2"
"CUSTOMER 1"
"CUSTOMER 2"
"CUSTOMER 1"
"CUSTOMER 2"

1 个答案:

答案 0 :(得分:12)

SELECT  customername
FROM    TEST
ORDER BY ROW_NUMBER() OVER ( PARTITION BY customername ORDER BY customername )