Presto SQL全外部联接

时间:2019-07-16 16:56:08

标签: sql presto

我有一个完全加入的成员,它无法正常运行。

我要加入4列。但是,其中一列具有一些空白值,从而导致出现一些数字[不联合]。当我删除此列时,查询有效。但是,我必须在报告中使用此列,因为这是我们在数字上进行报告的方式。

有疑问的查询:(无效的列是OM.line_item_id)

SELECT DBM.dated, DBM.line_item, DBM.line_item_id, DBM.insertion_order, DBM.insertion_order_id, DBM.device_type, DBM.market, DBM.impressions, DBM.clicks, DBM.amount_spent_EUR, OM.orders, OM.revenue, OM.device

FROM
(

SELECT
DATE_FORMAT(DATE_PARSE(date,'%Y/%m/%d'),'%Y-%m-%d') AS dated, line_item, line_item_id, insertion_order, insertion_order_id, device_type, trim(SPLIT_PART(insertion_order,'|',3)) AS market, cast(impressions as double) as impressions, cast(clicks as double) as clicks, CAST(media_cost_advertiser_currency AS DOUBLE)*1.15 AS amount_spent_EUR


FROM ralph_lauren_google_sheet_dbm_data  

WHERE dated >= '2019-03-31'
AND dated <= {{days_ago 1}}

GROUP BY 1,2,3,4,5,6,7,8,9,10

)DBM 

FULL outer JOIN

( 

SELECT dated, line_item_id, device, market, sum(orders) as orders, sum(revenue)+sum(shipping_revenue)-sum(coupon_discount) as revenue

FROM

(
select
dated,
utm_content_v21 as line_item_id,
order_currency_code_v33_evar33,
case lower(mobile_device_type)
when 'other' then 'Desktop'
when 'tablet' then 'Tablet'
when 'mobile phone' then 'Smart Phone'
        else 'Other'
        End as device,
case geosegmentation_countries
    when 'united kingdom' then 'UK'
    when 'germany' then 'DE'
    when 'france' then 'FR'
    when 'italy' then 'IT'
    when 'spain' then 'ES'
    else 'other'
    end as market,
sum(cast(orders as bigint))as orders,

            case
    WHEN lower(order_currency_code_v33_evar33) LIKE '%gbp%' THEN sum(TRY_CAST(revenue AS DOUBLE)*1.15)
    ELSE Sum(TRY_CAST(revenue AS DOUBLE)*1)
    END as revenue, 

    CASE
   WHEN lower(order_currency_code_v33_evar33) LIKE '%gbp%' THEN sum(TRY_CAST(order_level_shipping_revenue_e62_event62 AS DOUBLE)*1.15)
   ELSE sum(TRY_CAST(order_level_shipping_revenue_e62_event62 AS DOUBLE)*1)
   END as shipping_revenue,

CASE
    WHEN lower(order_currency_code_v33_evar33) LIKE '%gbp%' THEN sum(TRY_CAST(order_level_coupon_discount_e77_event77 AS DOUBLE)*1.15)  
    ELSE sum(TRY_CAST(order_level_coupon_discount_e77_event77 AS DOUBLE)*1)
    END as coupon_discount

from ralph_lauren_ftp_all_eu_markets_ltc  

WHERE dated >= '2019-03-31'
AND dated <= {{days_ago 1}}

and last_touch_channel like 'Retargeting' 

and lower(utm_medium_v21) not like '%fbig%'

and cast(orders as bigint) > 0


group by
1,2,3,4,5
)

GROUP BY 

1,2,3 ,4-- revenue numbers are getting duplicated for some reason

)OM

ON

DBM.dated = OM.dated AND DBM.line_item_id = OM.line_item_id and DBM.device_type = OM.device AND DBM.market = OM.market

如果其他三列联合起来,那么完全外部联接是否可以让我联接数字?

谢谢

0 个答案:

没有答案