雪花中的三张桌子

时间:2020-09-21 20:30:06

标签: sql snowflake-cloud-data-platform

我正在尝试在Snowflake中联接三个表,但在第三个表上却出错,并且不确定我是否使用了一个好的密钥:

import java.util.regex.Pattern;

class Test
{
    public static void main(String[] args)
    {
        String rawString = "junk_text?precious_text";
        String parsedString = Pattern.compile("\\?.+").matcher(rawString).group();
        System.out.println(parsedString); // need to print precious_text
    }
}

2 个答案:

答案 0 :(得分:1)

您的第二个左联接应上移到WHERE子句之前:

select
  a.CALENDAR_DATE
  , a.SALES_DOCUMENT
  , a.DOCUMENT_NUMBER_CROSS_BORDER
  , b.AUBEL
  , b.NETWR_ship
  , a.Delivery_time
  , a.Route_label
  , ue.order_ID
from DWHM_MALLGROUP.DM_LOG.SHIPMENTS a
left join (
  select
    AUBEL,
    sum(NETWR) as NETWR_ship
  from DWHM_MALLGROUP.SRC_SAP_ERP."2LIS_13_VDITM"
  where MATNR in ('10027', '10200', '10402', '10500', '10602', '10603', '10650', '10700')
  group by AUBEL
) b
  on a.DOCUMENT_NUMBER_CROSS_BORDER = b.AUBEL
left join "DWHM_MALLGROUP"."DM_COMMERCIAL"."UNIT_ECONOMICS" as ue
  ON a.SALES_DOCUMENT = ue.order_id
where
  a.CALENDAR_DATE >= '2020-04-01'
  and a.ROUTE in ('CZSK12', 'CZSK21', 'CZSK22', 'CZSK23', 'CZSK24', 'CZSK25', 'CZSK26', 'CZSK27', 'CZSK28', 'CZSK29', 'CZSK30', 'CZSK32', 'CZSK33', 'CZSK34', 'CZSK51', 'CZSK53', 'CZSK54', 'CZSK56')
and ue.country = 'SI'  and ue.delivery_date >= '2019-08-01'
group by a.CALENDAR_DATE, a.SALES_DOCUMENT, a.DOCUMENT_NUMBER_CROSS_BORDER, b.AUBEL, b.NETWR_ship, a.delivery_time, a.Route_LABEL

注意:我对您在SELECT子句中使用的*b并不熟悉...在我看来这是另一种错字,所以我将其删除。

如果您尝试在第二个LEFT JOIN之前执行汇总(GROUP BY),则可以使用CTE:

WITH CTE_1 AS (
select
  a.CALENDAR_DATE
  , a.SALES_DOCUMENT
  , a.DOCUMENT_NUMBER_CROSS_BORDER
  , b.AUBEL
  , b.NETWR_ship
  , a.Delivery_time
  , a.Route_label
from DWHM_MALLGROUP.DM_LOG.SHIPMENTS a
left join (
  select
    AUBEL,
    sum(NETWR) as NETWR_ship
  from DWHM_MALLGROUP.SRC_SAP_ERP."2LIS_13_VDITM"
  where MATNR in ('10027', '10200', '10402', '10500', '10602', '10603', '10650', '10700')
  group by AUBEL
) b
  on a.DOCUMENT_NUMBER_CROSS_BORDER = b.AUBEL
where
  a.CALENDAR_DATE >= '2020-04-01'
  and a.ROUTE in ('CZSK12', 'CZSK21', 'CZSK22', 'CZSK23', 'CZSK24', 'CZSK25', 'CZSK26', 'CZSK27', 'CZSK28', 'CZSK29', 'CZSK30', 'CZSK32', 'CZSK33', 'CZSK34', 'CZSK51', 'CZSK53', 'CZSK54', 'CZSK56')
group by a.CALENDAR_DATE, a.SALES_DOCUMENT, a.DOCUMENT_NUMBER_CROSS_BORDER, b.AUBEL, b.NETWR_ship, a.delivery_time, a.Route_LABEL
) c1
select
  c1.CALENDAR_DATE
  , c1.SALES_DOCUMENT
  , c1.DOCUMENT_NUMBER_CROSS_BORDER
  , c1.AUBEL
  , c1.NETWR_ship
  , c1.Delivery_time
  , c1.Route_label
  , ue.order_ID
left join "DWHM_MALLGROUP"."DM_COMMERCIAL"."UNIT_ECONOMICS" as ue
  ON c1.SALES_DOCUMENT = ue.order_id
and ue.country = 'SI'  and ue.delivery_date >= '2019-08-01'

这可以(而且应该是IMO)进行一些清理,但是我正在尝试解决您提出的有关联接3个表的问题。

答案 1 :(得分:0)

SQL存在多个问题。

, *bAUBEL
  , *b.NETWR_ship*

我们仅使用*从表中选择所有字段。例如select A.* , B.col1 from Table_a A , Table_B

加入的顺序是

  1. 选择
  2. 从表
  3. 加入其他表
  4. 加入条件
  5. 在哪里
  6. 组别
  7. 拥有

尝试使用此SQL

SELECT
    a.calendar_date,
    a.sales_document,
    a.document_number_cross_border,
    b.aubel,
    b.netwr_ship,
    a.delivery_time,
    a.route_label,
    ue.order_id
FROM
    dwhm_mallgroup.dm_log.SHIPMENTS a
left join (
  select
    AUBEL,
    sum(NETWR) as NETWR_ship
  from DWHM_MALLGROUP.SRC_SAP_ERP."2LIS_13_VDITM"
  where MATNR in ('10027', '10200', '10402', '10500', '10602', '10603', '10650', '10700')
  group by AUBEL
) b
  on a.DOCUMENT_NUMBER_CROSS_BORDER = b.AUBEL
  left join "DWHM_MALLGROUP"."DM_COMMERCIAL"."UNIT_ECONOMICS" as ue ON a.sales_document = ue.order_id
WHERE
    a.calendar_date >= '2020-04-01'
    AND a.route IN (
        'CZSK12',
        'CZSK21',
        'CZSK22',
        'CZSK23',
        'CZSK24',
        'CZSK25',
        'CZSK26',
        'CZSK27',
        'CZSK28',
        'CZSK29',
        'CZSK30',
        'CZSK32',
        'CZSK33',
        'CZSK34',
        'CZSK51',
        'CZSK53',
        'CZSK54',
        'CZSK56'
    )
        AND ue.country = 'SI'
            AND ue.delivery_date >= '2019-08-01'
GROUP BY
    a.calendar_date,
    a.sales_document,
    a.document_number_cross_border,
    b.aubel,
    b.netwr_ship,
    a.delivery_time,
    a.route_label
  ue.order_id