如何获取范围内的数据?

时间:2017-02-06 12:33:24

标签: sql database postgresql

我是SQL新手,下面的查询是计算在一段时间内行驶0到100公里的车辆数量。

select  1 as "1 - 100",count (*) from (
SELECT extract (day from start_time) as day ,place, vehicle_id,sum(distance_two_points) as distance
FROM public.datatable where start_time >= '2015-09-05 00:00:00' and start_time <= '2015-09-05 23:59:59' 
and place=1 group by day, place ,veh_id
order by day,place,veh_id ) as A where distance >0 and distance<100

这里distance_two_points显示一次行程中的距离,而且车辆的行程将超过行程。所以我为每个vehicle_id取得distance_two_points的总和,它给出了车辆行驶的总距离。

子查询

SELECT extract (day from start_time) as day ,place, vehicle_id,sum(distance_two_points) as distance
FROM public.datatable where start_time >= '2015-09-05 00:00:00' and start_time <= '2015-09-05 23:59:59' 
and place=1 group by day, place ,veh_id
order by day,place,veh_id

将返回所有车辆的总行驶距离,然后按

过滤
distance >0 and distance<100

我希望将其归类为以下内容,而不是一次过滤:

     range     count
     ______    ______
     1-100      17
     100-200    30
     300-400    40
     400-500    39
     500-600    36

有没有一种方法可以获得上述结果,而不是组合五个查询?任何帮助表示赞赏。

5 个答案:

答案 0 :(得分:1)

您可以使用case作为group by密钥。你似乎想要这样的东西:

SELECT (CASE WHEN points >= 0 AND points <= 100 THEN '1-100'
             WHEN points <= 200 THEN '101-200'
             WHEN points <= 300 THEN '201-300'
             WHEN points <= 400 THEN '301-400'
             WHEN points <= 500 THEN '401-500'
             WHEN points <= 600 THEN '501-600'
        END) as range,
       COUNT(*) as length
FROM public.datatable 
WHERE start_time >= '2015-09-05' and start_time < '2015-09-06' and
      place = 1 and length>=0 and length<=100 and place=1 
GROUP BY range
ORDER BY MIN(points);

答案 1 :(得分:0)

您可以使用函数来确定文本:

CREATE or replace FUNCTION points_diff(int)
 RETURNS text
 LANGUAGE sql

AS $function$

select
        a::text||'-'||(a+99)::text
from
        generate_series(1,$1+100,100) a
where
        $1 between a and a+99;

$function$

;

答案 2 :(得分:0)

WITH x AS (
     SELECT extract (day from start_time) as day,
            place,
            vehicle_id,
            sum(distance_two_points) as distance
       FROM public.datatable
      where start_time >= '2015-09-05 00:00:00'
        and start_time <= '2015-09-05 23:59:59' 
        and place=1
   group by day, place ,veh_id
), z AS (
     SELECT ((distance - 1) / 100)::int8 AS range,
            count (*)
       FROM x
   GROUP BY 1
)
  SELECT (range * 100 + 1)::text || '-' || ((range + 1) * 100),
         count
    FROM z
ORDER BY range

答案 3 :(得分:0)

案例工作正常。@ Gordon Linoff建议我必须使用案例,它就像一个魅力。希望它可以帮助任何人

<appender name="AzureTraceAppender" type="log4net.Appender.TraceAppender">
  <param name="ImmediateFlush" value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <!-- can be any pattern you like -->
    <conversionPattern value="%logger - %message" />
  </layout>
</appender>

答案 4 :(得分:-2)

where datediff (d,startdate, enddate) <= 100 
and startdate between startdate and enddate

参考:https://msdn.microsoft.com/en-us/library/bb510741.aspx