我正在尝试将数据(产品,时间戳)插入表格A,B和表格xyz中。 C使用主键作为PRODUCT。这三个表都有一个timestamp列和其他列。我想插入包含产品的行和这三个表(A,B& C)的最大时间戳。
答案 0 :(得分:0)
如果表xyz中已存在产品,则不指定要发生的情况,因此假设它不存在,您可以使用如下查询:
Insert into XYZ(Product, Timestamp)
Select Product, max(timestamp)
from (select product, timestamp from a union all
select product, timestamp from b union all
select product, timestamp from c)
group by product
答案 1 :(得分:0)
您可以尝试以下方法:
INSERT INTO xyz
( product, timestamp )
SELECT MAX(product) KEEP ( DENSE_RANK FIRST ORDER BY timestamp DESC ) AS product
, MAX(timestamp)
FROM a
UNION ALL
SELECT MAX(product) KEEP ( DENSE_RANK FIRST ORDER BY timestamp DESC ) AS product
, MAX(timestamp)
FROM b
UNION ALL
SELECT MAX(product) KEEP ( DENSE_RANK FIRST ORDER BY timestamp DESC ) AS product
, MAX(timestamp)
FROM c;
希望这有帮助。
答案 2 :(得分:-1)
更新:
试试这个(语法未经验证):
INSERT INTO xyz (product , timestamp)
(
SELECT product , MAX(A.timestamp)
FROM A
UNION
SELECT product , MAX(B.timestamp)
FROM B
UNION
SELECT product , MAX(C.timestamp)
FROM C
) ;