如何获取 sql 中两个日期之间所有列的不同行?

How to get distinct rows with all columns between two dates in sql?

我正在尝试获取两个日期之间具有唯一序列号的所有记录。但是我写不正确 sql.

这是我当前的 sql 查询和结果。有3条记录有两个XYZABC1230序列号。

select * from t_recorded_test
where  office_id = '1710011001123'
and    record_date > '2017-11-01'
and    record_date < '2017-12-08'
and    test_result = 'true';

MODEM_MODEL MODEM_SERIAL_NUMBER OFFICE_ID       RECORD_DATE             RECORD_ID                           TEST_RESULT
Type2ModelB XYZABC1230          1710011001123   2017-11-01 19:35:54.0   ccf57f20d585424abc9bce781ada9dcc    TRUE
Type2ModelB XYZABC1230          1710011001123   2017-11-01 19:33:54.0   168ce13ed9644f128f7769432ad6ba2f    TRUE
Type2ModelB XYZABC12312         1710011001123   2017-12-03 19:33:54.0   ab727f836c354f159703565b9eed3331    TRUE

但我希望只有两条记录 XYZABC1230 和 XYZABC12312。我该怎么做?

编辑:

对于所有相同的序列号,我只需要一个结果。日期不重要,但我必须用它来限制。

使用 GROUP BY 函数和 MAX(ROWID) 假设您想要按值分组的最新行。

SELECT * FROM T_RECORDED_TEST 
WHERE ROWID IN (
    SELECT MAX(ROWID)
    FROM T_RECORDED_TEST
    where office_id='1710011001123' 
    and record_date > '2017-11-01' 
    and record_date < '2017-12-08' 
    and test_result = 'true' 
    GROUP BY MODEM_SERIAL_NUMBER;
)

* 是罪魁祸首。如果您只对 MODEM_SERIAL_NUMBER 感兴趣,您可以使用如下的 distinct。

SELECT DISTINCT
    ( modem_serial_number )
FROM
    t_recorded_test
WHERE
    office_id = '1710011001123'
    AND   record_date > '2017-11-01'
    AND   record_date < '2017-12-08'
    AND   test_result = 'true';
--This is for MSSQL Server
    with CTE as(
    select *,(row_number() over (partition by MODEM_SERIAL_NUMBER order by MODEM_SERIAL_NUMBER)) 'ROW_N'  from T_RECORDED_TEST   
    where OFFICE_ID='1710011001123' and RECORD_DATE > '2017-11-01' and RECORD_DATE < '2017-12-08' and TEST_RESULT = 'TRUE')
    select MODEM_MODEL,MODEM_SERIAL_NUMBER,OFFICE_ID,RECORD_DATE,RECORD_ID,TEST_RESULT  from CTE where ROW_N=1