insert into tongqin(uid,date,area,pam,time)
select uuid_generate_v4(), tq.date::date, tq.area, tq.pam, substring(min(tq.time),12,8)::time as time
from(
select t.gid, t.time, substring(t.time,1,10) as date, t.daytime::float as weekDay,b.name as area,
case when substring(t.time, 11, 3)::float > 12 then 'PM' else 'AM' end as pam
from "trace" t, "testArea" b
where ST_Intersects(t.geom, b.geom) and t.daytime::bigint > 1 and t.daytime::bigint < 6
order by time
) tq
group by tq.date,tq.area, tq.pam
order by date, time