select m.p_id, m.aaa, m.bbb, m.ccc
from mytable m
inner join (
SELECT p_id, max(time1) max_date
FROM mytable
group by p_id
) t on t.max_date = m.time1 and t.p_id = m.p_id
或者,如果您只需要每个p\U id的最大值,您可以使用
select m.p_id, max(m.aaa), max(m.bbb), max(m.ccc )
from mytable m
group by m-p_id
SELECT t1.p_id,
(SELECT t2.aaa
FROM elbat t2
WHERE t2.p_id = t1.p_id
AND t2.aaa IS NOT NULL
ORDER BY t2.time1 DESC
LIMIT 1) aaa,
...
(SELECT t2.ccc
FROM elbat t2
WHERE t2.p_id = t1.p_id
AND t2.ccc IS NOT NULL
ORDER BY t2.time1 DESC
LIMIT 1) ccc,
FROM (SELECT DISTINCT
p_id
FROM elbat) t1;
3条答案
按热度按时间lndjwyie1#
如果需要与max\u daye相关的行,可以使用join和max time1的子查询
或者,如果您只需要每个p\U id的最大值,您可以使用
8cdiaqws2#
可以使用相关子查询:
nfg76nw03#
可以使用相关子查询为每个不同id的每一列获取“最新”的非空值。