在Hana中使用with子句创建表

时间:2018-12-19 07:19:02

标签: sql hana

我想使用with子句创建一个表:

例如:

with cte as
(SELECT B.STEUC "HSN",
        CASE WHEN A.FORMAT_CD='520' THEN '2' ELSE A.FORMAT_CD END FORMAT_CD,
        CASE WHEN A.FORMAT_CD='520' THEN 'DIGITAL' ELSE A.FORMAT_DESC END FORMAT_DESC,
        A.ARTICLE,
        A.REGION "STATE",
        SUM(CASE WHEN BWART IN ('702','704','708','711','713','715','717','551','553','555','903','909','951','Z09') THEN DMBTR ELSE 0 END) - 
        SUM(CASE WHEN BWART IN ('701','703','707','712','714','716','718','552','554',' 556','904','910','952','Z10') THEN DMBTR ELSE 0 END) "LOSS_VALUE"
FROM "_SYS_BIC"."RRA.DnL/CV_STOCK_MOVEMENT" A INNER JOIN "P22"."MARA" B ON A.ARTICLE=B.MATNR 
WHERE posting_date BETWEEN '20181101' AND '20181130' AND
      BWART IN ('702','704',' 708',' 711','713','715','717','701','703','707','712','714','716','718','551',
      '552','553','554','555','556','903','904','909','910','951','952','Z09','Z10') AND
      A.COMPANY_CODE='9008' AND
      A.LEVEL2 NOT IN ('10','99') AND
      A.LEVEL5 NOT IN ('140601010') AND
      A.FORMAT_CD NOT IN ('51','56','62','509')
GROUP BY B.STEUC,A.ARTICLE,A.REGION,A.COMPANY_CODE,A.FORMAT_CD,
        A.FORMAT_DESC)
SELECT A.HSN,
       A.STATE,
       A.FORMAT_CD,
       A.FORMAT_DESC,
       A.ARTICLE,
       A.LOSS_ART,
       B.LOSS
FROM (
SELECT A.HSN,
          A.STATE,
       A.FORMAT_CD,
       A.FORMAT_DESC,
       A.ARTICLE,
       A.LOSS LOSS_ART,
       SUM(A.LOSS) OVER (PARTITION BY A.HSN,A.STATE,A.FORMAT_CD ORDER BY  LOSS DESC) LOSS   
FROM (SELECT A.HSN,A.STATE,A.FORMAT_CD,A.FORMAT_DESC,A.ARTICLE,SUM(LOSS_VALUE) LOSS FROM 
--"RR_ANALYST"."REETIKA_LOSS_DATA_1" 
cte A 
INNER JOIN P22.MARA B ON A.ARTICLE=B.MATNR
      WHERE B.ATTYP<>'11'
      GROUP BY A.HSN,A.STATE,A.FORMAT_CD,A.FORMAT_DESC,A.ARTICLE 
      HAVING SUM(LOSS_VALUE)>0 ) A
      ) A ,
      (SELECT A.HSN,A.STATE,A.FORMAT_CD,SUM(LOSS_VALUE) LOSS FROM 
      cte  A 
      group by A.HSN,A.STATE,A.FORMAT_CD HAVING SUM(LOSS_VALUE)>0) B
  WHERE A.HSN=B.HSN AND
        A.STATE=B.STATE AND
        A.FORMAT_CD=B.FORMAT_CD AND
        A.LOSS<=B.LOSS*1

我猜Hana不支持哪个。

有什么可以替代的? hana是否像oracle一样支持假脱机处理?

我知道我可以导出结果集,然后相应地创建一个表。

但是有什么方法可以动态地实现和创建表吗?

1 个答案:

答案 0 :(得分:2)

另一种方法是 oldfashioned 方法-内联视图。

CREATE column TABLE t AS 
  SELECT * 
  FROM (SELECT 1 as some_value  --> this is your WITH factoring clause
        FROM dummy
        UNION ALL 
        SELECT 2 
        FROM dummy
      );