select DISTINCT COLLECT_TIME, PERF_VALUE, t1.unit, RULEA, RULEB, RULEC from ( select COLLECT_TIME, PERF_VALUE, UNIT as unit, EQP_OBJ_ID, OBJECT_TYPE, INDEX_TYPE, DEVICE_ID, INDEX_NAME from t_perf_sensor_history tpsh, t_perf_defined tpd where PERF_OBJ_ID = tpd.OBJ_ID ) as t1 left join t_alarm_rule tar on IF(tar.ALARM_OBJECT_TYPE = t1.DEVICE_ID, tar.ALARM_OBJECT_TYPE = t1.DEVICE_ID, tar.ALARM_OBJECT_TYPE = OBJECT_TYPE) where 1=1 and COLLECT_TIME BETWEEN DATE_SUB(NOW(), INTERVAL 62 DAY) AND now() order by COLLECT_TIME desc 怎末改才能避免重复数据
时间: 2023-08-08 15:08:34 浏览: 112
你可以在select子句中使用DISTINCT关键字去除重复数据,例如:
```
select DISTINCT COLLECT_TIME, PERF_VALUE, t1.unit, RULEA, RULEB, RULEC
from (
select COLLECT_TIME, PERF_VALUE, UNIT as unit, EQP_OBJ_ID, OBJECT_TYPE, INDEX_TYPE, DEVICE_ID, INDEX_NAME
from t_perf_sensor_history tpsh, t_perf_defined tpd
where PERF_OBJ_ID = tpd.OBJ_ID
) as t1
left join t_alarm_rule tar on IF(tar.ALARM_OBJECT_TYPE = t1.DEVICE_ID, tar.ALARM_OBJECT_TYPE = t1.DEVICE_ID, tar.ALARM_OBJECT_TYPE = OBJECT_TYPE)
where 1=1 and COLLECT_TIME BETWEEN DATE_SUB(NOW(), INTERVAL 62 DAY) AND now()
order by COLLECT_TIME desc
```
另外,你也可以使用 GROUP BY 子句来去除重复数据,例如:
```
select COLLECT_TIME, PERF_VALUE, t1.unit, RULEA, RULEB, RULEC
from (
select COLLECT_TIME, PERF_VALUE, UNIT as unit, EQP_OBJ_ID, OBJECT_TYPE, INDEX_TYPE, DEVICE_ID, INDEX_NAME
from t_perf_sensor_history tpsh, t_perf_defined tpd
where PERF_OBJ_ID = tpd.OBJ_ID
) as t1
left join t_alarm_rule tar on IF(tar.ALARM_OBJECT_TYPE = t1.DEVICE_ID, tar.ALARM_OBJECT_TYPE = t1.DEVICE_ID, tar.ALARM_OBJECT_TYPE = OBJECT_TYPE)
where 1=1 and COLLECT_TIME BETWEEN DATE_SUB(NOW(), INTERVAL 62 DAY) AND now()
group by COLLECT_TIME, PERF_VALUE, t1.unit, RULEA, RULEB, RULEC
order by COLLECT_TIME desc
```
注意,使用 GROUP BY 语句时需要将 select 子句中的每个非聚合列都包含在 GROUP BY 子句中。
阅读全文