我有这张桌子:
code | Seq | incr | Bin
------------------------
AQ | 2701 | 1 | 1
AQ | 2702 | 2 | 0
AQ | 2703 | 3 | 1
AQ | 2704 | 4 | 1
AQ | 2705 | 5 | 1
AQ | 2706 | 6 | 1
AQ | 2707 | 7 | 1
AQ | 2708 | 8 | 0
AQ | 2709 | 9 | 1
AQ | 2710 | 10 | 1
AQ | 2711 | 11 | 0
AQ | 2712 | 12 | 1
我需要获得连续数'1'的最大计数以及结果计数的“Seq”列中第一行和最后一行的值。即:对于书面简化表,最大计数为“5”,从“Seq”= 2703开始,以“Seq”= 2707结尾
所以我希望有一个结果表,应该是这样的:
code | SeqStart | SeqEnd | Count
----------------------------------
AQ | 2703 | 2707 | 5
AR | 2712 | 2722 | 11
试图解决这个问题我认为
“incr” - ( 行中的“incr”值,其中“Bin”= 0且最大“incr”但是<“incr” 对于“incr”从3到7为2,对于“incr”从9到9为8 10 )
可能是一个很好的数学方法来做到这一点但是(ofc,因为我在这里写)我找不到一种方法来为每行写入适当的第二个减法数字的工作代码
不幸的是,因为我使用OpenOffice(3.4.1),所以我坚持使用HSQLDB 1.8 如果有人能帮助我,我真的很感激,感谢您的阅读
答案 0 :(得分:1)
我自己只使用hsql 1.8找到了解决方案 - 如果其他人可能对它感兴趣,我会发布它:
SELECT "a"."code", ("a"."Seq"-"b"."MaxNum" +1) AS "SeqStart", "a"."Seq" AS "SeqEnd", "b"."MaxNum" AS "Count"
FROM
(SELECT DISTINCT "x"."code", "x"."Seq", "x"."incr", "x"."Bin", "y"."incr1", CASE WHEN "x"."Bin" = 1 THEN ("x"."incr"-"y"."incr1") ELSE '0' END AS "Num"
FROM "MyTable" AS "x"
LEFT JOIN
(SELECT "code", "incr", MAX("incr1") AS "incr1"
FROM
(SELECT "a"."code", "a"."incr", "b"."incr" AS "incr1"
FROM "MyTable" AS "a", "MyTable" AS "b"
WHERE ("b"."incr" BETWEEN '1' AND ("a"."incr" -1)) AND "b"."Bin" = 0)
GROUP BY "code", "incr") AS "y"
ON "y"."code" = "x"."code" AND "y"."incr" = "x"."incr") AS "a"
INNER JOIN
(SELECT "code", MAX("Num") AS "MaxNum"
FROM
(SELECT DISTINCT "x"."code", "x"."Seq", "x"."incr", "x"."Bin", "y"."incr1", CASE WHEN "x"."Bin" = 1 THEN ("x"."incr"-"y"."incr1") ELSE '0' END AS "Num"
FROM "MyTable" AS "x"
LEFT JOIN
(SELECT "code", "incr", MAX("incr1") AS "incr1"
FROM
(SELECT "a"."code", "a"."incr", "b"."incr" AS "incr1"
FROM "MyTable" AS "a", "MyTable" AS "b"
WHERE ("b"."incr" BETWEEN '1' AND ("a"."incr" -1)) AND "b"."Bin" = 0)
GROUP BY "code", "incr") AS "y"
ON "y"."code" = "x"."code" AND "y"."incr" = "x"."incr")
GROUP BY "code") AS "b"
ON "b"."code" = "a"."code" AND "b"."MaxNum" = "a"."Num"
答案 1 :(得分:0)
我只想添加上面查询的调优版本,速度提高了15倍:
SELECT "a"."code", ("a"."Seq"-"b"."MaxNum" +1) AS "SeqStart", "a"."Seq" AS "SeqEnd", "b"."MaxNum" AS "Count"
FROM
(SELECT DISTINCT "x"."code", "x"."Seq", "x"."incr", "x"."Bin", "y"."incr1", CASE WHEN "x"."Bin" = 1 THEN ("x"."incr"-"y"."incr1") ELSE '0' END AS "Num"
FROM
"MyTable" AS "x"
LEFT JOIN
(SELECT "code", "incr", MAX("incr1") AS "incr1"
FROM
(SELECT "a"."code", "a"."incr", "b"."incr" AS "incr1"
FROM
"MyTable" AS "a"
INNER JOIN
( SELECT "code", "incr", "Bin"
FROM
"MyTable"
WHERE "Bin" = 0 ) AS "b"
ON "b"."code" = "a"."code" AND ( "b"."incr" BETWEEN '1' AND ( "a"."incr" - 1 ) ) )
GROUP BY "code", "incr") AS "y"
ON "y"."code" = "x"."code" AND "y"."incr" = "x"."incr") AS "a"
INNER JOIN
(SELECT "code", MAX("Num") AS "MaxNum"
FROM
(SELECT DISTINCT "x"."code", "x"."Seq", "x"."incr", "x"."Bin", "y"."incr1", CASE WHEN "x"."Bin" = 1 THEN ("x"."incr"-"y"."incr1") ELSE '0' END AS "Num"
FROM
"MyTable" AS "x"
LEFT JOIN
(SELECT "code", "incr", MAX("incr1") AS "incr1"
FROM
(SELECT "a"."code", "a"."incr", "b"."incr" AS "incr1"
FROM
"MyTable" AS "a"
INNER JOIN
( SELECT "code", "incr", "Bin"
FROM
"MyTable"
WHERE "Bin" = 0 ) AS "b"
ON "b"."code" = "a"."code" AND ( "b"."incr" BETWEEN '1' AND ( "a"."incr" - 1 ) ) )
GROUP BY "code", "incr") AS "y"
ON "y"."code" = "x"."code" AND "y"."incr" = "x"."incr")
GROUP BY "code") AS "b"
ON "b"."code" = "a"."code" AND "b"."MaxNum" = "a"."Num"