从SQL中的多个数据获取最短时间

时间:2019-01-31 00:37:18

标签: sql

SELECT
    invlod.lodnum, 
    dlytrn.trndte
FROM  
    invlod
INNER JOIN
    dlytrn ON invlod.lodnum = dlytrn.lodnum
WHERE
    invlod.stoloc = (@stoloc) 
    AND dlytrn.actcod = 'PALPCK' 
    AND dlytrn.oprcod = 'PCK'
ORDER BY 
    dlytrn.trndte

结果输出:

00100370000510204922    1/24/2019 7:28:26 AM    
00100370000510204922    1/24/2019 7:28:44 AM    
00100370000510204939    1/24/2019 7:28:57 AM    
00100370000510204939    1/24/2019 7:29:12 AM    
00100370008030047708    1/24/2019 7:37:01 AM    
00100370008030047708    1/24/2019 7:37:01 AM    

我需要唯一的(最小)时间戳。如果有重复,我只需要一个输出。

2 个答案:

答案 0 :(得分:1)

您需要像这样group by

select invlod.lodnum, min(dlytrn.trndte) as trndte
from invlod
inner join dlytrn on invlod.lodnum = dlytrn.lodnum
where invlod.stoloc = (@stoloc) and dlytrn.actcod= 'PALPCK' and dlytrn.oprcod = 'PCK'
group by invlod.lodnum
order by trndte

答案 1 :(得分:1)

在oracle

Select distinct 
invlod.lodnum, 
 min ( dlytrn.trndte) over ( partition by invlod.lodnum) min_trndte
FROM invlod
inner join dlytrn on invlod.lodnum = dlytrn.lodnum
where invlod.stoloc = (@stoloc) and dlytrn.actcod= 'PALPCK' and dlytrn.oprcod = 'PCK'
Order by dlytrn.trndte