Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用预定义的值顺序选择列值_Sql_Postgresql - Fatal编程技术网

Sql 使用预定义的值顺序选择列值

Sql 使用预定义的值顺序选择列值,sql,postgresql,Sql,Postgresql,我有一个包含这些值的表,我想用可选值显示结果,我还想更改值的顺序,如customer 1、customer 3、customer 2,但它们必须是可选顺序。 客户名称 CREATE TABLE TEST ( customername varchar(50) ); INSERT INTO TEST VALUES('CUSTOMER 1'); INSERT INTO TEST VALUES('CUSTOMER 1'); INSERT INTO T

我有一个包含这些值的表,我想用可选值显示结果,我还想更改值的顺序,如customer 1、customer 3、customer 2,但它们必须是可选顺序。 客户名称

    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');
    INSERT INTO TEST VALUES('CUSTOMER 3');
    INSERT INTO TEST VALUES('CUSTOMER 3');
    INSERT INTO TEST VALUES('CUSTOMER 3');
预期结果:

    CUSTOMER 2
    CUSTOMER 1
    CUSTOMER 3
    CUSTOMER 2
    CUSTOMER 1
    CUSTOMER 3
    CUSTOMER 2
    CUSTOMER 1
    CUSTOMER 3
我试过:

    SELECT  customername
    FROM    TEST
    ORDER BY ROW_NUMBER() OVER ( PARTITION BY customername ORDER BY  customername)
这就回来了,

    CUSTOMER 2
    CUSTOMER 1
    CUSTOMER 3
    CUSTOMER 2
    CUSTOMER 1
    CUSTOMER 3
    CUSTOMER 1
    CUSTOMER 3
    CUSTOMER 2

N.B值可以是3,2,1这样的整数,因为上面只是一个示例

您需要一个定义所需顺序的附加表:

create table dict(customername varchar(50), priority int);
insert into dict values
('CUSTOMER 2', 1),
('CUSTOMER 1', 2),
('CUSTOMER 3', 3);

select customername
from test
join dict
using (customername)
order by
    row_number() over (partition by customername),
    priority;

 customername 
--------------
 CUSTOMER 2
 CUSTOMER 1
 CUSTOMER 3
 CUSTOMER 2
 CUSTOMER 1
 CUSTOMER 3
 CUSTOMER 2
 CUSTOMER 1
 CUSTOMER 3
(9 rows)    

是否为不同顺序的
定义了任何角色
,或者我们可以假设总是2,3,1?