PARTITION BY语法错误 - 如何获取分区结果集

时间:2018-04-13 09:03:07

标签: mysql sql database partition sql-function

我的查询:

SELECT education,nickname,height
    ROW_NUMBER() OVER (PARTITION BY education ORDER BY height DESC) INDEX_NUM
FROM user
LIMIT 100;

我的表格定义:

CREATE TABLE user (

female BOOLEAN NOT NULL,

nickname VARCHAR(60) NOT NULL,
id INT NOT NULL,
charm INT NOT NULL,
standardmember BOOLEAN NOT NULL,
idcard BOOLEAN NOT NULL,

age INT NOT NULL,
marriage VARCHAR(5) NOT NULL,
region VARCHAR(10) NOT NULL,

education VARCHAR(10) NOT NULL,
height INT NOT NULL,
weight INT NOT NULL,
constellation VARCHAR(5),
race VARCHAR(5),
animalzodiac VARCHAR(2) NOT NULL,

intro TEXT NOT NULL,

rage VARCHAR(10) NOT NULL,
rheight VARCHAR(15) NOT NULL,
rrace VARCHAR(10) NOT NULL,
reducation VARCHAR(10) NOT NULL,
rphoto VARCHAR(5) NOT NULL,
rmarriage VARCHAR(10) NOT NULL,
rregion VARCHAR(100) NOT NULL,
rhonesty VARCHAR(10) NOT NULL,

jobtitle VARCHAR(15),
industry VARCHAR(20),
companynature VARCHAR(10),
employeewelfare VARCHAR(10),
workintensity VARCHAR(10),
mobility VARCHAR(10),
careervsfamily VARCHAR(10),
overseaassignment VARCHAR(10),

college VARCHAR(30),
majortype VARCHAR(5),
languages VARCHAR(50),

birthplace VARCHAR(10),
residence VARCHAR(10),
nationality VARCHAR(15),
personality VARCHAR(5),
humor VARCHAR(6),
temper VARCHAR(6),
seriousness VARCHAR(15),
kidwish VARCHAR(5),
whentomarry VARCHAR(5),
longdistancelove VARCHAR(5),
dreammarriage TEXT,

livewithparents VARCHAR(5),
parents VARCHAR(5),
siblings VARCHAR(25),
financialsituation VARCHAR(10),
parentsinsurance VARCHAR(10),
parentsemployment VARCHAR(35),

sent BOOLEAN NOT NULL DEFAULT 0;

PRIMARY KEY (id)

);

我的目标是划分结果集并将它们排序为高度升序。像这样:

my goal

但是我得到了错误:

syntax error

嗯,我对MySQL和查询语法不太满意。如果我问一个愚蠢的问题,请随意教我。 THX。

1 个答案:

答案 0 :(得分:0)

尝试此查询:

SELECT education,
       nickname,
       height,
       ROW_NUMBER() OVER w AS INDEX_NUM
FROM u
WINDOW w AS (
             PARTITION BY education,
             nickname ORDER BY height DESC 
             );

这里我使用ROW_NUMBER和Window函数。 由于MySQL支持仅用作窗口函数的非聚合函数。对于这些,OVER子句是强制性的,如:

CUME_DIST()
DENSE_RANK()
FIRST_VALUE()
LAG()
LAST_VALUE()
LEAD()
NTH_VALUE()
NTILE()
PERCENT_RANK()
RANK()
ROW_NUMBER()

有关详细信息,请访问以下链接:

  

https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html