Min()返回的值高于max()

时间:2015-10-24 20:58:42

标签: sql sqlite aggregate-functions

我在SQLite中使用此查询

SELECT
  x.Level,
  x.Parent,
  x.Ranking,
  y.minr,
  y.maxr
FROM
  GroupRanking AS x
INNER JOIN
(
  SELECT
    Level,
    Parent,
    min(Ranking) AS minr,
    max(Ranking) AS maxr
  FROM
    GroupRanking
  GROUP BY
    Level, Parent
) AS y
ON
  x.Level = y.Level AND x.Parent = y.Parent
ORDER BY
  x.Level, x.Parent, x.Ranking
LIMIT 16;

观察结果

   Level Parent     Ranking        minr        maxr
1      1      1 -1691668126 -1691668126  -763167442
2      1      1  1805402547 -1691668126  -763167442
3      1      1   331841336 -1691668126  -763167442
4      1      1  -763167442 -1691668126  -763167442
5      1      2 -1649664922 -1649664922 -1698226108
6      1      2  1050628156 -1649664922 -1698226108
7      1      2  1195643104 -1649664922 -1698226108
8      1      2 -1698226108 -1649664922 -1698226108
9      1      3   742489555   742489555 -1068497810
10     1      3   613872842   742489555 -1068497810
11     1      3   267740911   742489555 -1068497810
12     1      3 -1068497810   742489555 -1068497810
13     1      4  1053184739  1053184739  -437919960
14     1      4  -843952369  1053184739  -437919960
15     1      4  -769107202  1053184739  -437919960
16     1      4  -437919960  1053184739  -437919960

min()max()的结果是错误的,也是观察的顺序。问题似乎与订单有关。

以下是GroupRanking

的示例
CREATE TABLE Base (BaseValue);
INSERT INTO Base (BaseValue) VALUES (1);
INSERT INTO Base (BaseValue) VALUES (2);
INSERT INTO Base (BaseValue) VALUES (3);
INSERT INTO Base (BaseValue) VALUES (4);

CREATE TABLE GroupRanking AS 
SELECT
  x1.BaseValue AS Level,
  x2.BaseValue AS Parent,
  x3.BaseValue AS Child,
  random() AS Ranking
FROM
  Base AS x1
CROSS JOIN
  Base AS x2
CROSS JOIN
  Base AS x3;

0 个答案:

没有答案