0

我希望在 SQL 查询中创建一个案例,并根据条件分配几个结果。

例如 :

代码 :

INSERT INTO DESTINATION_TABLE (DT_TRT, NU_QUARTER, NU_YEAR) VALUES
  (SELECT
   CASE
    WHEN #P_DT_TRT# = '1900-00-00'
    THEN MAX(TT.DT_CTTT)
    ELSE #P_DT_TRT#
   END AS DT_TRT,
   CASE
   WHEN EXTRACT (MONTH FROM DT_TRT) < 4
    THEN NU_QUARTER = 4 AND NU_YEAR = EXTRACT (YEAR FROM DT_TRT) - 1
    ELSE NU_YEAR = EXTRACT (YEAR FROM DT_TRT)
   END
   CASE
    WHEN EXTRACT (MONTH FROM DT_TRT) < 7
    THEN 1
    ELSE (CASE WHEN EXTRACT (MONTH FROM DT_TRT) < 10 THEN 2 ELSE 3 END AS NU_QUARTER)
    END AS NU_QUARTER
   FROM TARGET_TABLE TT);

算法 :

-> A date will be given in the programme to enable the calculation (#P_DT_TRT#)
If the parameter is not supplied (value = 1900-00-00)
DT_TRT = the largest constitution date (DT_CTTT) in the target table (TARGET_TABLE TT)
Otherwise DT_TRT = date given in parameter
If DT_TRT month < 4
Quarter = 4
Year = Year of DT_TRT - 1
Otherwise Year = Year of DT_TRT
If DT_TRT month < 7
Quarter = 1
Otherwise
If DT_TRT < 10
Quarter = 2
Otherwise Quarter = 3

问题:是否可以在一种情况下整合多个结果(DT_TRT、NU_QUARTER、NU_YEAR)?如果是这样,语法是什么?

我在 Teradata Studio 工作。

谢谢您的回答。:)

4

1 回答 1

0

这似乎是你的逻辑:

INSERT INTO DESTINATION_TABLE (DT_TRT, NU_QUARTER, NU_YEAR) 
VALUES
 (
   -- If the parameter is not supplied (value = 1900-00-00)
   -- DT_TRT = the largest constitution date (DT_CTTT) in the target table (TARGET_TABLE TT)
   -- Otherwise DT_TRT = date given in parameter
   CASE
     WHEN #P_DT_TRT# = '1900-00-00'
     THEN (SELECT Max(DT_CTTT) FROM TARGET_TABLE)
     ELSE #P_DT_TRT#
   END,
   
   -- shift back year/quarter by three months to adjust for company's business year
   td_quarter_of_year(Add_Months(DT_TRT, -3)),
   Extract(YEAR From Add_Months(DT_TRT, -3))
 )
;
于 2021-05-18T14:53:02.720 回答