SQL查询帮助计算最大值

问题描述 投票:0回答:1

我有一个关于在SQL中需要执行的查询的问题(我使用BQ)。

我有这张桌子:

train_no, wagon_no, weight, length, date, startpoint(km), endpoint(km)
1, 123, 1000, 20, 20190101, 0, 7
1, 234, 2000, 20, 20190101, 1, 2
1, 345, 3000, 30, 20190101, 1, 5
1, 456, 1000, 40, 20190101, 1, 6
2, 987, 1000, 10, 20190101, 0, 8
2, 876, 2000, 20, 20190101, 1, 2
2, 765, 3000, 20, 20190101, 1, 5
2, 654, 1000, 20, 20190101, 1, 6

表格显示了两列有货车的火车。对于每辆货车,我们看到货车在什么时候被添加到火车上。因此,对于1号火车,我们看到从起点= 1(公里1)到终点= 2(公里2)的货车234被包含在火车上,然后将其从火车上移开。我们还看到,对于train_no = 1,最大终点为7,因此火车的最大行驶距离为7公里。

总的火车长度和重量会随着距离的变化而变化,我想计算出在这段距离内达到的最大长度和最大重量。如何在SQL中执行此操作?

任何建议都值得赞赏。

编辑:添加图片以澄清我正在寻找什么。如您在所附图片中所见,Train_no = 1的最大重量介于点1和2之间。总重量为7000,是该特定距离下火车中所有货车的总和。同样,总长度为110,这是所有加在一起的货车的总长度。enter image description here

sql google-bigquery data-modeling bigquery
1个回答
0
投票

下面是BigQuery标准SQL的内容>>

#standardSQL
WITH temp AS (
  SELECT train_no, dt, MIN(startpoint) startpoint, MAX(endpoint) endpoint
  FROM `project.dataset.table` 
  GROUP BY train_no, dt
)
SELECT train_no, dt, MAX(wagons) max_wagons, MAX(total_weight) AS max_total_weight, MAX(total_len) max_total_len
FROM (
  SELECT train_no, dt, point, COUNT(wagon_no) wagons, SUM(weight) total_weight, SUM(len) total_len 
  FROM temp, UNNEST(GENERATE_ARRAY(startpoint, endpoint)) point
  LEFT JOIN `project.dataset.table` t
  USING(train_no, dt)
  WHERE point >= t.startpoint AND point < t.endpoint
  GROUP BY train_no, dt, point
)
GROUP BY train_no, dt   

如果适用于您的问题的样本数据,如下例所示>>

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 train_no, 123 wagon_no, 1000 weight, 20 len, '20190101' dt, 0 startpoint, 7 endpoint UNION ALL
--       1, 123, 1000, 20, '20190101', 0, 7
  SELECT 1, 234, 2000, 20, '20190101', 1, 2 UNION ALL
  SELECT 1, 345, 3000, 30, '20190101', 1, 5 UNION ALL
  SELECT 1, 456, 1000, 40, '20190101', 1, 6 UNION ALL
  SELECT 2, 987, 1000, 10, '20190101', 0, 8 UNION ALL
  SELECT 2, 876, 2000, 20, '20190101', 1, 2 UNION ALL
  SELECT 2, 765, 3000, 20, '20190101', 1, 5 UNION ALL
  SELECT 2, 654, 1000, 20, '20190101', 1, 6 
), temp AS (
  SELECT train_no, dt, MIN(startpoint) startpoint, MAX(endpoint) endpoint
  FROM `project.dataset.table` 
  GROUP BY train_no, dt
)
SELECT train_no, dt, MAX(wagons) max_wagons, MAX(total_weight) AS max_total_weight, MAX(total_len) max_total_len
FROM (
  SELECT train_no, dt, point, COUNT(wagon_no) wagons, SUM(weight) total_weight, SUM(len) total_len 
  FROM temp, UNNEST(GENERATE_ARRAY(startpoint, endpoint)) point
  LEFT JOIN `project.dataset.table` t
  USING(train_no, dt)
  WHERE point >= t.startpoint AND point < t.endpoint
  GROUP BY train_no, dt, point
)
GROUP BY train_no, dt   

结果是

Row train_no    dt          max_wagons  max_total_weight    max_total_len    
1   1           20190101    4           7000                110  
2   2           20190101    4           7000                70   

以下查询返回每个KM标记在每个火车上以降序排序的长度。

with data as (
  select 1 as train_no, 123 as wagon_no, 1000 as weight, 20 as length, 20190101 as date, 0 as startpoint, 7 as endpoint union all
  select 1, 234, 2000, 20, 20190101, 1, 2 union all
  select 1, 345, 3000, 30, 20190101, 1, 5 union all
  select 1, 456, 1000, 40, 20190101, 1, 6 union all
  select 2, 987, 1000, 10, 20190101, 0, 8 union all
  select 2, 876, 2000, 20, 20190101, 1, 2 union all
  select 2, 765, 3000, 20, 20190101, 1, 5 union all
  select 2, 654, 1000, 20, 20190101, 1, 6
),
km_array as (
  select * from unnest(generate_array(0,10)) km
),
joined as (
  select *
  from km_array
  cross join data
  where km between startpoint and endpoint
),
train_length_at_each_km as (
  select
    km,
    train_no,
    sum(length) as length
  from joined
  group by 1,2
)
select
  train_no, length, km
from train_length_at_each_km 
order by train_no, length desc

获得最大权重将使用与train_length_at_each_km CTE类似的逻辑。


0
投票

以下查询返回每个KM标记在每个火车上以降序排序的长度。

© www.soinside.com 2019 - 2024. All rights reserved.