SQL 在 where 子句中使用 MAX 进行子选择转换为 Informix

SQL Subselect with MAX in where clause convert to Informix

我在 MSSQL 中有以下查询:

select
    c1.auftrnr
    ,MAX(DATUM) as Datum
    ,(select MAX(Zeit) from kaufedit where auftrnr=c1.auftrnr and datum=max(c1.datum)) as Zeit
from kaufedit c1
group by c1.auftrnr
order by c1.auftrnr

这将为每个订单选择最长时间,并为该最长时间选择最长时间。为了检查是否发生了变化。这真的很好用而且很快。

我正在尝试将它转换成 Informix,但直到现在我还无法让它工作。

更新 1

12.10.FC14WE是版本 基准是日期(10) 时间是 char(5) 两者都不能更改:(

我试过以下方法:

select extend(datum, year to second)+(zeit - DATETIME(00:00) hour to minute) as datumzeit from kaufedit

IMO,如果您将 Datum 和 Zeit 列合并为一个 DATETIME YEAR TO SECOND 值,那将是最好的。您只需要每个订单的组合列的最大值。

但是,假设这不是一个选项并且您确实拥有足够新的 Informix 版本,那么您可以使用 CTE — 常见的 table 表达式 — 如下所示:

WITH t1 AS
    (SELECT auftrnr, MAX(datum) AS datum
       FROM kaufedit
      GROUP BY auftrnr
    )
SELECT c1.auftrnr,
       c1.datum,
       MAX(c1.zeit) AS zeit
  FROM kaufedit AS c1
  JOIN t1 ON t1.auftrnr = c1.auftrnr AND t1.datum = c1.datum
 GROUP BY c1.auftrnr, c1.datum
 ORDER BY c1.auftrnr, c1.datum;

CTE 推导出订单的订单号和最大日期。然后主查询将该结果与主查询 table 连接起来,产生所需的输出。

架构和数据 数据格式假定您在环境中设置了 DBDATE='Y4MD-' 或等效项。或者,将 DATE 类型更改为 DATETIME YEAR TO DAY。

DROP TABLE IF EXISTS kaufedit;

CREATE TABLE kaufedit
(
    auftrnr     INTEGER NOT NULL,
    datum       DATE NOT NULL,
    zeit        DATETIME HOUR TO SECOND NOT NULL,
    information VARCHAR(32) NOT NULL
);

INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(123, '2022-01-01', '01:23:45', 'Entered at 2022-01-01T01:23:45');
INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(123, '2022-01-01', '10:22:44', 'Entered at 2022-01-01T10:22:44');
INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(123, '2022-01-01', '11:32:54', 'Entered at 2022-01-01T11:32:54');
INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(123, '2022-01-01', '21:17:05', 'Entered at 2022-01-01T21:17:05');

INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(321, '2022-01-02', '01:23:45', 'Entered at 2022-01-02T01:23:45');
INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(321, '2022-01-02', '17:22:44', 'Entered at 2022-01-02T17:22:44');
INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(321, '2022-01-03', '11:32:54', 'Entered at 2022-01-03T11:32:54');
INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(321, '2022-01-03', '11:57:05', 'Entered at 2022-01-03T11:57:05');

INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(444, '2022-02-02', '01:23:45', 'Entered at 2022-02-02T01:23:45');
INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(444, '2022-03-02', '10:22:44', 'Entered at 2022-03-02T10:22:44');
INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(444, '2022-04-03', '11:32:54', 'Entered at 2022-04-03T11:32:54');
INSERT INTO kaufedit(auftrnr, datum, zeit, information)
    VALUES(444, '2022-05-03', '21:57:05', 'Entered at 2022-05-03T21:57:05');

输出

123  2022-01-01  21:17:05
321  2022-01-03  11:57:05
444  2022-05-03  21:57:05

(在 Linux RHEL 7.4 和 Informix 14.10.FC1 上测试。)


分机

如果您想要 kaufedit table 中的额外信息,那么您可以使用如下查询:

WITH t1 AS
    (SELECT auftrnr, MAX(datum) AS datum
       FROM kaufedit
      GROUP BY auftrnr
    ),
     t2 AS
    (SELECT c1.auftrnr, c1.datum, MAX(c1.zeit) AS zeit
       FROM kaufedit AS c1
       JOIN t1 ON t1.auftrnr = c1.auftrnr AND t1.datum = c1.datum
      GROUP BY c1.auftrnr, c1.datum
    )
SELECT c1.auftrnr, c1.datum, c1.zeit, c1.information
  FROM kaufedit AS c1
  JOIN t2
    ON c1.auftrnr = t2.auftrnr
   AND c1.datum = t2.datum
   AND c1.zeit = t2.zeit
 ORDER BY c1.auftrnr, c1.datum;

输出

123  2022-01-01  21:17:05  Entered at 2022-01-01T21:17:05
321  2022-01-03  11:57:05  Entered at 2022-01-03T11:57:05
444  2022-05-03  21:57:05  Entered at 2022-05-03T21:57:05

问题是:

日期为日期(10) 时间是 char(5)

解决方案是:

select TO_DATE(TO_CHAR(datum,%d-%m-%Y)|| ' ' || || zeit || ':00',"%d-%m-%Y %H:%M:%S))

这对 MAX 和分组依据非常有效。