根据列值创建max_value表

时间:2018-12-03 07:49:58

标签: sql amazon-redshift

我正在尝试使用不同的表来计算新值,但是首先我需要创建一个具有此计算限制的辅助表,以实现更高的模块化并能够在需要时修改限制。

我想要一张这样的桌子:

product_type      product_area    max_limit
product-100_sp         71            67.5
product-100_sp         70            22.5
product-200_se         71             80
product-200_se         70             16

我正在尝试运行此

CREATE OR REPLACE VIEW aux_limits AS
 SELECT DISTINCT
 mtd.product_type,
 v.product_area,
 CASE WHEN (v.product_area=70 AND mtd.product_type LIKE '%product-100%' THEN 22.5 ELSE NULL END) AS Max_Limit
 CASE WHEN (v.product_area=71 AND mtd.product_type LIKE '%product-100%' THEN 67.5 ELSE NULL END) AS Max_Limit
 CASE WHEN (v.product_area=70 AND mtd.product_type LIKE '%product-200%' THEN 16 ELSE NULL END) AS Max_Limit
 CASE WHEN (v.product_area=71 AND mtd.product_type LIKE '%product-200%' THEN 80 ELSE NULL END) AS Max_Limit
 CASE WHEN (v.product_area=70 AND mtd.product_type LIKE '%product-300%' THEN 40 ELSE NULL END) AS Max_Limit
 CASE WHEN (v.product_area=71 AND mtd.product_type LIKE '%product-300%' THEN 70 ELSE NULL END) AS Max_Limit
 CASE WHEN (v.product_area=70 AND mtd.product_type LIKE '%product-400%' THEN 40 ELSE NULL END) AS Max_Limit
 CASE WHEN (v.product_area=71 AND mtd.product_type LIKE '%product-400%' THEN 340 ELSE NULL END) AS Max_Limit
 CASE WHEN (v.product_area=70 AND mtd.product_type LIKE '%product-500%' THEN 25 ELSE NULL END) AS Max_Limit
 CASE WHEN (v.product_area=71 AND mtd.product_type LIKE '%product-500%' THEN 40 ELSE NULL END) AS Max_Limit
FROM table1 AS v, table2 AS mtd
WHERE (product_area=71 OR product_area=70)
GROUP BY 1,2

mtd的结构是:

local_id global_id product_name    project_name      product_type
  745      896      example1         example1        product-100_test

v的结构是

product_key product_area      timestamp             database_id     value 
   8552          71      2016-01-01 00:00:00             3          45325
   8550          105      2016-01-01 00:00:00            3          68

但是我遇到以下错误:

SQL Error [500310] [42601]: [Amazon](500310) Invalid operation: syntax error at or near "THEN" 
Position: 211;
  [Amazon](500310) Invalid operation: syntax error at or near "THEN" 
Position: 211;
  [Amazon](500310) Invalid operation: syntax error at or near "THEN" 
Position: 211;

老实说我是SQL的新手,所以我认为自己做错了什么,但是我不知道这是什么

0 个答案:

没有答案