我正在尝试获取以下查询的结果:
select distinct
REQ_ID
, ID
, MAX(STEP) as step
, SUBSTRING(p1.NAME,9, len(p1.NAME)) as _index_
, p2.value as location
from HISTORY h
LEFT JOIN Parameter p1
on p1.WP_ID=h.ID
AND ( ( p1.NAME like 'name_'
AND p1.VALUE like h.ID COLLATE DATABASE_DEFAULT )
OR ( p1.NAME like 'name__'
AND p1.VALUE like h.ID COLLATE DATABASE_DEFAULT) )
LEFT JOIN Parameter p2
on p2.WP_ID=h.ID
AND p2.PA_NAME = 'Location' + (SUBSTRING(p1.NAME,9, len(p1.NAME)) )
WHERE h.ROLE = 'rock'
GROUP BY REQ_ID, ID, step, p1.name, p2.value
问题是查询返回超过 1 个结果(在我的情况下有 6 个)并且MAX(STEP)
值没有返回最大值,我可以看到像 0,1,3,0,1,2 这样的值有没有办法只得到结果max(step)
?step
领域是varchar(1)