我很难过这个。我是初学者,也许我已经深入了解联盟。作为一种方法,我分别为结果的不同功能编写了查询。我现在正在尝试合并,它正在破碎。
我使用Impala查询并在使用HIVE导入HDFS之前在MySQL中创建表。
有效的查询是:
SELECT carrier, zone, speed, min_price, price
FROM (SELECT carrier, total_wt, zone, speed, price, MIN(price) OVER(PARTITION BY speed) as min_price
FROM shipping_prices
WHERE total_wt IN (
SELECT
SUM(prod.shipping_wt)
FROM order_details ordet
JOIN products prod
ON prod.prod_id = ordet.prod_id
WHERE ordet.order_id = 5841506
GROUP BY ordet.order_id)
AND zone IN (
SELECT cal_zone
FROM (
SELECT carrier, dest_zip, origin_zip, zone, MIN(zone) OVER(PARTITION BY carrier) as cal_zone
FROM shipping_zones
WHERE (origin_zip = 402 OR origin_zip = 950) AND dest_zip = '560'
) t
WHERE zone=cal_zone)
) z
WHERE price=min_price
ORDER BY speed DESC;
返回:
+---------+------+-------+-----------+-------+
| carrier | zone | speed | min_price | price |
+---------+------+-------+-----------+-------+
| fedex | 4 | slow | 10.86 | 10.86 |
| usps | 4 | med | 11.15 | 11.15 |
| usps | 4 | fast | 40.55 | 40.55 |
+---------+------+-------+-----------+-------+
我遇到问题的查询似乎在最后结果之前的步骤失败了,当它应该找到9个价格总计,每个级别每个运营商一个。相反,它返回27,因此每个级别每个运营商找到9个价格。这没有意义,因为每个级别每个运营商只有3个价格。这是查询:
SELECT lev5.zone, lev5.cust_id, lev5.total_wt, lev5.zip_shrt, lev5.order_id, lev5.carrier, pric.speed, pric.price, MIN(price) OVER(PARTITION BY speed) as min_price
FROM (
SELECT lev4.cust_id, lev4.total_wt, lev4.zip_shrt, lev4.order_id, lev4.carrier, lev4.zone
FROM (
SELECT lev3.cust_id, lev3.total_wt, lev3.zip_shrt, lev3.order_id, zon.carrier, zon.origin_zip, zon.dest_zip, zon.zone, MIN(zon.zone) OVER(PARTITION BY zon.carrier) as calc_zone
FROM (
SELECT lev2.cust_id, SUM(shipping_wt)/2 AS total_wt, STRLEFT(lev2.zipcode, 3) AS zip_shrt, lev2.order_id
FROM (
SELECT lev1.zipcode, lev1.cust_id, lev1.order_id, ordet.prod_id
FROM (
SELECT cus.zipcode, cus.cust_id, ord.order_id
FROM orders ord
JOIN customers cus ON ord.cust_id = cus.cust_id
WHERE ord.order_id = 5841506
) lev1
JOIN order_details ordet
ON lev1.order_id = ordet.order_id
WHERE ordet.order_id = lev1.order_id
) lev2
JOIN products prod
ON lev2.prod_id = prod.prod_id
GROUP BY lev2.cust_id, zip_shrt, lev2.order_id
) lev3
JOIN shipping_zones zon
ON lev3.zip_shrt = zon.dest_zip
WHERE (origin_zip = 402 OR origin_zip = 950) AND dest_zip = lev3.zip_shrt
) lev4
WHERE lev4.zone = calc_zone
) lev5
JOIN shipping_prices pric
ON lev5.zone = pric.zone AND lev5.total_wt = pric.total_wt;
返回:
+------+---------+----------+----------+----------+---------+-------+-------------------+-----------+
| zone | cust_id | total_wt | zip_shrt | order_id | carrier | speed | price | min_price |
+------+---------+----------+----------+----------+---------+-------+-------------------+-----------+
| 4 | 1050349 | 4 | 560 | 5841506 | ups | med | 22.24 | 11.15 |
| 4 | 1050349 | 4 | 560 | 5841506 | ups | med | 23.57 | 11.15 |
| 4 | 1050349 | 4 | 560 | 5841506 | fedex | med | 11.15 | 11.15 |
| 4 | 1050349 | 4 | 560 | 5841506 | ups | med | 11.15 | 11.15 |
| 4 | 1050349 | 4 | 560 | 5841506 | usps | med | 22.24 | 11.15 |
| 4 | 1050349 | 4 | 560 | 5841506 | fedex | med | 23.57 | 11.15 |
| 4 | 1050349 | 4 | 560 | 5841506 | usps | med | 23.57 | 11.15 |
| 4 | 1050349 | 4 | 560 | 5841506 | usps | med | 11.15 | 11.15 |
| 4 | 1050349 | 4 | 560 | 5841506 | fedex | med | 22.24 | 11.15 |
| 4 | 1050349 | 4 | 560 | 5841506 | fedex | slow | 11.15 | 10.86 |
| 4 | 1050349 | 4 | 560 | 5841506 | fedex | slow | 11.15 | 10.86 |
| 4 | 1050349 | 4 | 560 | 5841506 | fedex | slow | 10.86 | 10.86 |
| 4 | 1050349 | 4 | 560 | 5841506 | usps | slow | 11.15 | 10.86 |
| 4 | 1050349 | 4 | 560 | 5841506 | usps | slow | 11.15 | 10.86 |
| 4 | 1050349 | 4 | 560 | 5841506 | usps | slow | 10.86 | 10.86 |
| 4 | 1050349 | 4 | 560 | 5841506 | ups | slow | 11.15 | 10.86 |
| 4 | 1050349 | 4 | 560 | 5841506 | ups | slow | 11.15 | 10.86 |
| 4 | 1050349 | 4 | 560 | 5841506 | ups | slow | 10.86 | 10.86 |
| 4 | 1050349 | 4 | 560 | 5841506 | ups | fast | 69.93000000000001 | 40.55 |
| 4 | 1050349 | 4 | 560 | 5841506 | ups | fast | 40.55 | 40.55 |
| 4 | 1050349 | 4 | 560 | 5841506 | ups | fast | 65.94 | 40.55 |
| 4 | 1050349 | 4 | 560 | 5841506 | usps | fast | 69.93000000000001 | 40.55 |
| 4 | 1050349 | 4 | 560 | 5841506 | usps | fast | 40.55 | 40.55 |
| 4 | 1050349 | 4 | 560 | 5841506 | usps | fast | 65.94 | 40.55 |
| 4 | 1050349 | 4 | 560 | 5841506 | fedex | fast | 69.93000000000001 | 40.55 |
| 4 | 1050349 | 4 | 560 | 5841506 | fedex | fast | 40.55 | 40.55 |
| 4 | 1050349 | 4 | 560 | 5841506 | fedex | fast | 65.94 | 40.55 |
+------+---------+----------+----------+----------+---------+-------+-------------------+-----------+
我进入excel确认(shipping_prices的源数据)并执行了COUNTIFS函数,它只返回了9行。
我真的很感激一些帮助。我自己一直试图解决这个问题,但也许是因为我缺乏连接知识或其他我没有经验的东西。我为自己感到非常自豪,我已经到了这一点,但这个障碍正在扼杀我。
谢谢
答案 0 :(得分:0)
这就是我最终得到的结果:
SELECT lev6.order_id, lev6.carrier, lev6.speed, lev6.min_price AS cheapest_price
FROM (
SELECT lev5.zone, lev5.cust_id, lev5.total_wt, lev5.zip_shrt, lev5.order_id, lev5.origin_zip, lev5.carrier, pric.speed, pric.price, MIN(pric.price) OVER(PARTITION BY speed) as min_price
FROM (
SELECT lev4.cust_id, lev4.total_wt, lev4.zip_shrt, lev4.order_id, lev4.carrier, lev4.zone, lev4.origin_zip
FROM (
SELECT lev3.cust_id, lev3.total_wt, lev3.zip_shrt, lev3.order_id, zon.carrier, zon.origin_zip, zon.dest_zip, zon.zone, MIN(zon.zone) OVER(PARTITION BY zon.carrier) as calc_zone
FROM (
SELECT lev2.cust_id, SUM(shipping_wt)/2 AS total_wt, STRLEFT(lev2.zipcode, 3) AS zip_shrt, lev2.order_id
FROM (
SELECT lev1.zipcode, lev1.cust_id, lev1.order_id, ordet.prod_id
FROM (
SELECT cus.zipcode, cus.cust_id, ord.order_id
FROM orders ord
JOIN customers cus ON ord.cust_id = cus.cust_id
WHERE ord.order_id = 6453746
) lev1
JOIN order_details ordet
ON lev1.order_id = ordet.order_id
WHERE ordet.order_id = lev1.order_id
) lev2
JOIN products prod
ON lev2.prod_id = prod.prod_id
GROUP BY lev2.cust_id, zip_shrt, lev2.order_id
) lev3
JOIN shipping_zones zon
ON lev3.zip_shrt = zon.dest_zip
WHERE (origin_zip = 402 OR origin_zip = 950) AND dest_zip = lev3.zip_shrt
) lev4
WHERE lev4.zone = calc_zone
) lev5
JOIN shipping_prices pric
ON lev5.zone = pric.zone AND lev5.total_wt = pric.total_wt
WHERE lev5.carrier = pric.carrier
) lev6
WHERE lev6.price = lev6.min_price
ORDER BY lev6.speed DESC;
我的问题是我的lev5上没有WHERE条件。我补充说:
WHERE lev5.carrier = pric.carrier
这消除了重复,并正确地返回了9个运费,每个运营商3个(慢,中,快)。然后我添加lev6只返回每个速度的最低价格。
@Thorsten Kettner至于为什么我必须将产品重量的总和除以2,遗憾的是我没有答案。如果我没有,它会返回订单实际重量的两倍,不知道为什么。
这个查询不是防弹,也没有针对空值和所有情况进行测试(比如当两个价格在相同的速度下相等时),但它足以满足我想做的事情,并证明了概念。