我不知道从许多具有相同ID并与另一个表连接的记录中获取最大记录值。我希望有人能在这里帮助我。
这个表:
order_id name result1 result2 result3 max_date admin seq2 extern
517 A 97 65 77 23-DEC-14 Rey 2 null
617 B null null null null 5 6
517 A null null null 18-DEC-14 Roo 1 2
617 B 44 78 80 22-DEC-14牵引6 5
我想要的结果是这样的:
order_id name result1 result2 result3 max_date admin seq2 extern
517 A 97 65 77 23-DEC-14 Rey 2 2
617 B 44 78 80 22-DEC-14牵引6 6
我尝试这个查询,但它只给出了一个结果:
WITH t AS
(SELECT x.order_id,x.name,x.extern_order_status as extern,y.result1, y.result2, y.result3,
t_logging_max.xd1 as max_date,
p_agent_admin.username as admin,
MAX(y.seq) AS seq2
FROM t_order_demand x
LEFT OUTER JOIN
(
SELECT id,xn1, MAX(xd1) AS xd1
FROM t_logging
GROUP BY id,xn1
) t_logging_max ON x.order_id = t_logging_max.id
LEFT JOIN
(
SELECT agent_id,username
FROM p_agent
GROUP BY agent_id,username
) p_agent_admin ON t_logging_max.xn1 = p_agent_admin.agent_id
JOIN t_order_log y
ON x.order_id = y.order_id
and y.order_id LIKE '%10%'
GROUP BY x.order_id,x.name,x.extern_order_status,y.result1, y.result2,y.result3,t_logging_max.xd1,p_agent_admin.username)
SELECT * FROM t WHERE (t.seq2) IN (SELECT MAX(tt.seq2) FROM t tt);
实际上,必须有超过2条id %10%的记录
希望你能帮助我。感谢。
答案 0 :(得分:1)
Group by
的 Max Aggregate
应该有效。除了Admin
列,其中包含varchar
值。
SELECT order_id,
NAME,
Max(result1) result1,
Max(result2) result2,
Max(result3) result3,
Max(max_date) max_date,
Max(admin) admin,
Max(seq2) seq2,
Max(extern) extern
FROM tablename
GROUP BY order_id,
NAME