查询给出错误的结果。知道为什么会这样吗?

时间:2019-03-06 11:14:38

标签: sql amazon-redshift etl

即使在ETL Manager中成功运行,查询也不会返回任何行。知道为什么会这样吗?如果您认为前两个临时表可能包含零个结果,由于我分别执行了每个部分,因此我可以保证它们不会。

 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

1 个答案:

答案 0 :(得分:0)

检查表之间的格式意外差异。

建议将以下部分表示为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')
…