我有以下带有列的表戳:
LOC(Blob)
LOCDTL
LOCTIME
CARRIERTYPE
CARRIERID
我想获取与每个ID相对应的行,该ID来自查询本身数组中前端的每个ID。
我已经尝试过这种可能性,但没有一种能按照我想要的方式工作。
SELECT X(LOC), Y(LOC), LOCDTL, LOCTIME, UPPER(CARRIERTYPE), CARRIERID
FROM LOC_EVENT_CARRIER_REPO
WHERE UPPER(TRIM(CARRIERTYPE)) = "VESSEL"
AND UPPER(TRIM(CARRIERID)) IN (35, 69,70,71,72,73,74)
AND LOCTIME IN (SELECT MAX(LOCTIME) FROM LOC_EVENT_CARRIER_REPO
WHERE UPPER(CARRIERTYPE) = "VESSEL"
AND CARRIERID IN (35, 69,70,71,72,73,74))
group by CARRIERID;
答案 0 :(得分:1)
如果我做对了。找到最后一行,在提供的ID列表中为每个MAX(LOCTIME)
添加CARRIERID
SELECT X(LOC), Y(LOC), LOCDTL, LOCTIME, UPPER(CARRIERTYPE), CARRIERID
FROM LOC_EVENT_CARRIER_REPO e
JOIN (
SELECT CARRIERID, MAX(LOCTIME) maxlt
FROM LOC_EVENT_CARRIER_REPO
WHERE UPPER(CARRIERTYPE) = "VESSEL"
AND CARRIERID IN (35, 69,70,71,72,73,74)
GROUP BY CARRIERID
) m ON e.CARRIERID = m.CARRIERID AND e.LOCTIME = m.maxlt
答案 1 :(得分:0)
如果我理解正确,则只需要一个相关子句,而不是子查询中的GROUP BY
:
SELECT X(LECR.LOC), Y(LECR.LOC), LECR.LOCDTL,
LECR.LOCTIME, UPPER(LECR.CARRIERTYPE), LECR.CARRIERID
FROM LOC_EVENT_CARRIER_REPO LECR
WHERE UPPER(TRIM(LECR.CARRIERTYPE)) = 'VESSEL' AND
LECR.CARRIERID IN (35, 69, 70, 71, 72, 73, 74) AND
LECR.LOCTIME IN (SELECT MAX(LECR2.LOCTIME)
FROM LOC_EVENT_CARRIER_REPO LECR2
WHERE UPPER(TRIM(LECR2.CARRIERTYPE)) = UPPER(TRIM(LECR.CARRIERTYPE)) AND
LECR2.CARRIERID = LECR.CARRIERID
);
您的版本与所有运营商上的最长时间匹配,而不仅仅是逐个运营商。
注意:
UPPER(TRIM())
。UPPER(TRIM())
,而CARRIERID
看起来是数字。GROUP BY
。