查询给出错误的结果。知道为啥会这样吗?
Posted
技术标签:
【中文标题】查询给出错误的结果。知道为啥会这样吗?【英文标题】:Query giving wrong results. Any idea why is this happening?查询给出错误的结果。知道为什么会这样吗? 【发布时间】:2019-03-06 11:14:37 【问题描述】:尽管在 ETL 管理器中成功运行,但查询没有返回任何行。知道为什么会这样吗?如果您认为前两个临时表可能包含零结果,我可以保证它们不会,因为我已经分别执行了每个部分。
CREATE TEMPORARY TABLE T1 AS /* This is to get top 1000 address ids which are unique identifiers for addresses in terms of orders frequency which is decided by number of distinct ordering order ids */
SELECT destination_address_id
,COUNT(DISTINCT ordering_order_id)a
FROM oe
where shipment_status = 'DELIVERED'
AND delivery_station_code = 'DCH1'
AND event_day BETWEEN '2018-10-01' AND '2018-12-31'
AND tracking_id IS NOT NULL
GROUP BY destination_address_id
ORDER BY a DESC
LIMIT 1000
;
CREATE TEMPORARY TABLE T1_2 AS /* This is to get tracking ids corresponding to those top 1000 address ids */
SELECT DISTINCT destination_address_id
,tracking_id
FROM oe
WHERE destination_address_id IN (SELECT destination_address_id FROM T1)
AND event_day BETWEEN '2018-10-01' AND '2018-12-31'
AND shipment_status = 'DELIVERED'
AND delivery_station_code = 'DCH1'
AND tracking_id IS NOT NULL
;
SELECT DISTINCT lat1
,long1
,external_address_id destination_address_id
,tracking_id
,actual_lat
,actual_long
,ROW_NUMBER() OVER(PARTITION BY tracking_id ORDER BY deliverydate DESC) rn
FROM gdd
WHERE shipment_status_id in (51,'DELIVERED')
AND tracking_id IN(SELECT tracking_id FROM T1_2)
AND confidence1 = 'high'
AND station_code='DCH1'
AND deliverydate BETWEEN TO_DATE('2018-10-01','YYYY-MM-DD') AND TO_DATE('2018-12-31','YYYY-MM-DD')
AND actual_lat IS NOT NULL
AND actual_long IS NOT NULL
【问题讨论】:
您在最终选择中查找为:AND station_code='DCH1' 而您之前选择 AND delivery_station_code = 'DCH1' 这可能是您的问题吗? 都是一样的,只是不同表的列名不同 只需通过依次注释掉每个 where 条件来分解它,直到找到原因(Joakim 可能是正确的) 【参考方案1】:检查表格之间的意外格式差异。
建议将以下部分表示为 JOIN 而不是 IN 列表。然后您可以暂时将其更改为左连接,以查看是否有 任何行匹配。
你的 SQL:
…
FROM gdd
WHERE shipment_status_id in (51,'DELIVERED')
AND tracking_id IN(SELECT tracking_id FROM T1_2)
…
加入 SQL:
…
FROM gdd
INNER | LEFT JOIN T1_2 ON gdd.tracking_id = T1_2.tracking_id
WHERE shipment_status_id in (51,'DELIVERED')
…
【讨论】:
是的,这似乎是问题所在,但不清楚原因。以上是关于查询给出错误的结果。知道为啥会这样吗?的主要内容,如果未能解决你的问题,请参考以下文章