You can use the GROUP BY clause along with the HAVING clause to limit duplicates based on two fields. Here is the modified query statement:

SELECT
    tbl_punchrecord.FRegNo,
    tbl_punchrecord.FDate,
    tbl_baseinfo.FEmpName,
    tbl_baseinfo.FDept,
    tbl_punchrecord.FPunchCode,
    tbl_punch.FName,
    tbl_punchrecord.FLocation
FROM
    tbl_punchrecord,
    tbl_baseinfo,
    tbl_punch
WHERE
    tbl_punchrecord.FPunchCode = tbl_punch.FCode
    AND tbl_punchrecord.FRegNo = tbl_baseinfo.FRegNo
    AND FDate BETWEEN '2023-08-01' AND '2023-08-31'
GROUP BY
    tbl_punchrecord.FRegNo,
    tbl_punchrecord.FDate,
    tbl_baseinfo.FEmpName,
    tbl_baseinfo.FDept,
    tbl_punchrecord.FPunchCode,
    tbl_punch.FName,
    tbl_punchrecord.FLocation
HAVING
    COUNT(*) > 1
ORDER BY
    FDate ASC,
    FRegNo ASC

This will return records that have duplicate FRegNo and FDate values.

SQL DISTINCT with Group BY and HAVING: How to Remove Duplicate Records Based on Two Fields

原文地址: https://www.cveoy.top/t/topic/hb0T 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录