**我想获取员工最后一次刷卡和第一次刷卡的刷卡信息。 根据我的查询结果,它返回了大量每天发生两次以上的刷卡交易数据。
WITH PERSO AS (
SELECT MUSTERI,MAX(KART_BASIM_DATE) TARIH
FROM PERSO_BASVURU PR
GROUP BY MUSTERI) ,
ISLEMLER_MAXARAC AS (
SELECT PR.MUSTERI,M.ID ,M.ADI NAME,M.SOYADI SURNAME, NO_KART,MAX(TRST_ISLEM) MAXTRST
FROM PERSO PR
JOIN PERSO_BASVURU BB ON BB.MUSTERI=PR.MUSTERI
JOIN PERSO_MUSTERI M ON BB.MUSTERI=M.ID
JOIN S_DETAY_ARAC S ON BB.KART_NO=S.NO_KART
WHERE S.TRST_ISLEM BETWEEN
TO_DATE('05.12.2023 000000', 'DD.MM.YYYY HH24MISS') AND TO_DATE('08.12.2023 230000' , 'DD.MM.YYYY HH24MISS') AND
S.ID_TIP_BILET IN (114) AND S.NO_ARAC BETWEEN 6000 AND 7000
GROUP BY PR.MUSTERI,M.ID,M.ADI,M.SOYADI,NO_KART
) ,
ISLEMLER_MINARAC AS (
SELECT MIN(TRST_ISLEM) MINTRST
FROM PERSO PR
JOIN PERSO_BASVURU BB ON BB.MUSTERI=PR.MUSTERI AND PR.TARIH=BB.KART_BASIM_DATE
JOIN PERSO_MUSTERI M ON BB.MUSTERI=M.ID
JOIN S_DETAY_ARAC S ON BB.KART_NO=S.NO_KART
WHERE S.TRST_ISLEM BETWEEN
TO_DATE('05.12.2023 000000', 'DD.MM.YYYY HH24MISS') AND TO_DATE('08.12.2023 230000' , 'DD.MM.YYYY HH24MISS')
AND S.ID_TIP_BILET IN (114) AND S.NO_ARAC BETWEEN 6000 AND 7000
GROUP BY PR.MUSTERI,M.ADI,M.SOYADI,NO_KART)
SELECT X.ID, X.ADI, X.SOYADI, N.MINTRST,X.MAXTRST
FROM ISLEMLER_MAXARAC X
JOIN ISLEMLER_MINARAC N
ON TO_DATE(N.MINTRST,'DD.MM.YYYY')=TO_DATE(X.MAXTRST,'DD.MM.YYYY')
WHERE X.MAXTRST-N.MINTRST>0
GROUP BY X.ADI, X.SOYADI , N.MINTRST,X.MAXTRST
您可以使用
MIN
和 MAX
聚合函数并从 MINTRST
中删除列 MAXTRST
和 GROUP BY
,如下所示:
SELECT X.ID, X.ADI, X.SOYADI,
MIN(N.MINTRST),
MAX(X.MAXTRST)
FROM ISLEMLER_MAXARAC X
JOIN ISLEMLER_MINARAC N
ON TO_DATE(N.MINTRST,'DD.MM.YYYY')=TO_DATE(X.MAXTRST,'DD.MM.YYYY')
WHERE X.MAXTRST-N.MINTRST>0
GROUP BY X.ID, X.ADI, X.SOYADI