sql 1
获得这一年的数据,以年以及设备名称分组,进行sql语句的编写
select
DATE_FORMAT(time,"%Y") years,id,name,time,SUM(d_variation_plane_x) d_variation_plane_x,SUM(d_variation_plane_y)
d_variation_plane_y,SUM(d_variation_plane_h) d_variation_plane_h
FROM smos_module_gnss_data
<where>
<if test="date != null and date != ''">
YEAR(time) = #{date}
</if>
AND calculating_type = 1 AND statistics_type = 0 AND flag = 0
</where>
GROUP BY years,name ORDER BY time
DATE_FORMAT(time,“%Y”) 将time格式化为年份,还可以有其他的写法,如%Y-%m 为月,等等。
分组为GROUP BY years,name,先按年分组,按年分组之后再按设备名name分组
结果如下:

查出来按2021分组之后,再按照设备名进行分组。
sql 2
数据库删除重复记录 保留id最小那行记录
工作中遇到的问题,做下笔记省得忘记
delete from testname
where (name,age) in (select * from (select name,age from testname group by name,age having count(*) > 1) b
)
and (id) not in (select * from (select min(id) from testname group by name,age having count(*)>1) c)

本文介绍了如何使用SQL查询获取特定年份并按设备名分组的数据,以及如何删除数据库中重复记录并保留id最小的行。示例SQL语句展示了GROUP BY和HAVING子句的应用,以及在处理重复数据时的策略。

225

被折叠的 条评论
为什么被折叠?



