1、如何用一个开始时间一个结束时间为条件,查询一个时间段内的数据
sql=" select * from tablename where 字段名称 between '"& 开始日期变量 &"' and '"& 截止日期变量 &"'"
sql=" select * from tablename where 字段名称 between '"& StartTime &"' and '"& StopTime &"'"
2、SQL的复杂查询
SELECT rates.description,
COUNT(vendor_id) as num_trips,
AVG(dropoff_datetime - pickup_datetime) as avg_trip_duration,
AVG(total_amount) as avg_total,
AVG(tip_amount) as avg_tip,
MIN(trip_distance) as min_distance,
AVG(trip_distance) as avg_distance,
MAX(trip_distance) as max_distance,
AVG(passenger_count) as avg_passengers
FROM rides
JOIN rates on rides.rate_code = rates.rate_code
WHERE rides.rate_code in (2,3)
AND pickup_datetime < '2016-02-01'
GROUP BY rates.description
ORDER BY rates.description;
3、查询某段时间之内的所有数据(时间可改) --- postgresql timescaledb 数据库
select * from metrics where xxx_time < Now() - interval '6 days' order by xxx_time
4、两个表联查 单个字段在某段时间之内的 --- postgresql timescaledb 数据库
select * from 表名1 left join 表名2 on 表名1.字段 = 表名2.字段 where 表名1.字段 =?1 and xxx_time < Now() - interval '30 minutes' order by xxxx_time
注意:这里第二个表名1.字段是我这个题目“单个字段” , 第一个 表名1.字段是和第二张表相关联的字段,切勿混淆!!