我如下创建了一个临时表T3_final。我想数一数。在destination_address_id级别的跟踪ID的集合。但是,数字被夸大了(对于特定的目标地址id为73)。为什么会这样?
编辑:更新了问题详细信息。所提供的图像具有针对该特定目的地址ID的16个跟踪ID,但最终结果显示了70个以上的跟踪ID。添加了包含T3和路由临时表的sql代码,还包括直到最终输出的代码。
T3表的样本数据集(按tracking_id排序)
| lat1 | long1 | actual_lat | actual_long | tracking_id | destination_address_id | distance |
|-----------|------------|------------|-------------|-----------------|------------------------|----------|
| 41.867318 | -87.625199 | 41.866985 | -87.625177 | TBA291146931000 | 57137334115 | 37.11 |
| 41.699121 | -87.685905 | 41.699075 | -87.685903 | TBA294192909000 | 52320567 | 5.08 |
| 41.839378 | -87.64605 | 41.839463 | -87.646085 | TBA294636573000 | 60406350705 | 9.95 |
| 41.893888 | -87.960975 | 41.893863 | -87.960985 | TBA295159303000 | 85918178503 | 2.87 |
| 41.801833 | -87.587964 | 41.802045 | -87.587743 | TBA295399226000 | 1.41975E+11 | 29.88 |
| 41.867318 | -87.625199 | 41.867463 | -87.625158 | TBA295579366000 | 57137334115 | 16.44 |
| 41.823113 | -87.773061 | 41.823028 | -87.772978 | TBA295889245000 | 609866840 | 11.7 |
| 41.880609 | -87.643551 | 41.880643 | -87.643825 | TBA296616086000 | 73353853103 | 23 |
| 41.879607 | -87.787703 | 41.87942 | -87.787827 | TBA297207405000 | 1.52921E+11 | 23.22 |
路线表的样本数据集
| route_id | tracking_id |
|-------------|-----------------|
| 5606318-230 | TBA291146931000 |
| 5615178-242 | TBA291146931000 |
| 5608371-231 | TBA291146931000 |
| 5619196-12 | TBA294192909000 |
| 5615178-249 | TBA294636573000 |
| 5615178-15 | TBA295159303000 |
| 5615178-266 | TBA295399226000 |
| 5608971-29 | TBA295399226000 |
| 5616765-168 | TBA295579366000 |
| 5615178-43 | TBA295889245000 |
| 5611715-191 | TBA296616086000 |
| 5615178-239 | TBA296616086000 |
| 5613961-24 | TBA296616086000 |
| 5615178-46 | TBA297207405000 |
| 5615178-45 | TBA297381148000 |
| 5615178-327 | TBA297914514000 |
| 5615178-122 | TBA298006586000 |
| 5615178-220 | TBA298012265000 |
| 5615178-203 | TBA298150824000 |
| 5615178-4 | TBA298172704000 |
当前最终输出(此目标地址ID的软件包数应为16个错误):
| zero_to_ten_percentage | eleven_to_twenty_percentage | twenty_to_fifty_percentage | gt_50_percentage | destination_address_id | route_id | total_packages |
|------------------------|-----------------------------|----------------------------|------------------|------------------------|-------------|----------------|
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5726011-18 | 73 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5132076-67 | 73 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5741216-13 | 73 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5722811-179 | 73 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5647148-145 | 73 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5701985-94 | 73 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5657147-148 | 73 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5713302-122 | 73 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5627191-48 | 73 |
所需的输出:
| zero_to_ten_percentage | eleven_to_twenty_percentage | twenty_to_fifty_percentage | gt_50_percentage | destination_address_id | route_id | total_packages |
|------------------------|-----------------------------|----------------------------|------------------|------------------------|-------------|----------------|
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5726011-18 | 16 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5132076-67 | 16 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5741216-13 | 16 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5722811-179 | 16 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5647148-145 | 16 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5701985-94 | 16 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5657147-148 | 16 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5713302-122 | 16 |
| 50.6849315 | 41.0958904 | 4.109589 | 0 | 100529310603 | 5627191-48 | 16 |
CREATE TEMPORARY TABLE T3 AS
SELECT DISTINCT T2_2.lat1
,T2_2.long1
,T2_2.actual_lat
,T2_2.actual_long
,T2_2.tracking_id
,T2_2.destination_address_id
,round((CASE WHEN ((lat1 IS NOT NULL AND lat1 != '')
AND (lat1 = actual_lat)
AND (actual_long IS NOT NULL AND actual_long != '') AND (long1 = actual_long)) THEN 0
WHEN ((lat1 IS NOT NULL AND lat1 != '')
AND (long1 IS NOT NULL AND long1 != '')
AND (actual_lat IS NOT NULL AND actual_lat != '')
AND (actual_long IS NOT NULL AND actual_long != ''))
THEN (ACOS(ROUND(COS(ACOS(- 1) / 180 * (90 - lat1)) * COS(ACOS(- 1) / 180 * (90 - actual_lat))
+ SIN(ACOS(- 1) / 180 * (90 -lat1)) * SIN(ACOS(- 1) / 180 * (90 - actual_lat)) * COS(ACOS(- 1) / 180
* (long1 - actual_long)), 20)) * 6378137
) ELSE NULL END
), 2) distance
FROM T2_2
GROUP BY 1, 2, 3 , 4, 5, 6
ORDER BY tracking_id
;
CREATE TEMPORARY TABLE route AS
SELECT DISTINCT route_id,
tracking_id
FROM routing_na.actuals
WHERE tracking_id IN (SELECT DISTINCT tracking_id FROM T3)
ORDER BY tracking_id
;
CREATE TEMPORARY TABLE T3_final AS
SELECT DISTINCT distance
,T3.tracking_id
,destination_address_id
,route_id
FROM T3, route
WHERE T3.tracking_id = route.tracking_id
ORDER BY T3.tracking_id
;
CREATE TEMPORARY TABLE packages AS
SELECT COUNT(DISTINCT tracking_id) packs
,destination_address_id
FROM T3_final
GROUP by destination_address_id
;
CREATE TEMPORARY TABLE T4 AS /* Doing some percentage calculations based on the new buckets created in the previous temp table namely percentage calculations out of total */
SELECT SUM(CASE WHEN distance BETWEEN 0 AND 10 THEN 1 ELSE 0 END)a
,SUM(CASE WHEN distance BETWEEN 11 AND 20 THEN 1 ELSE 0 END)b
,SUM(CASE WHEN distance BETWEEN 21 AND 50 THEN 1 ELSE 0 END)c
,SUM(CASE WHEN distance > 50 THEN 1 ELSE 0 END)d
,packages.destination_address_id
,a/packs::DECIMAL(10,2) * 100 AS e
,b/packs::DECIMAL(10,2) * 100 AS f
,c/packs::DECIMAL(10,2) * 100 AS g
,d/packs::DECIMAL(10,2) * 100 AS h
,packs AS Total_packages
FROM T3_final, packages
WHERE T3_final.destination_address_id = packages.destination_address_id
GROUP BY packages.destination_address_id,packs
ORDER BY packages.destination_address_id
;
SELECT DISTINCT e zero_to_ten_percentage
,f eleven_to_twenty_percentage
,g twenty_to_fifty_percentage
,h gt_50_percentage
,T4.destination_address_id
,route_id
,Total_packages
FROM T3_final, T4
WHERE T3_final.destination_address_id = T4.destination_address_id
ORDER BY T4.destination_address_id
;
Actual no of tracking ids for a particular destination_address_id