SELECT有多个WHERE子句。

时间:2015-01-17 02:21:42

标签: sql teradata

帮助!我无法弄清楚为什么我收到此错误消息:SELECT有多个WHERE子句。

错误必须在sfd表和我正在联合的select语句中,因为在我做出一些更改之前查询有效。

SELECT snapshot_date,
inv.sku  ||  inv.warehouse_id,
inv.permalink,
inv.fulfillment_method,
CASE
  WHEN inv.warehouse_id = 'ads' THEN 'ADS (SC)'
  WHEN inv.warehouse_id = 'inoh' THEN 'Innotrac (OH)'
  WHEN inv.warehouse_id = 'innv' THEN 'Innotrac (NV)'
  WHEN inv.warehouse_id = 'inil' THEN 'Innotrac (IL)'
  WHEN inv.warehouse_id = 'kfc' THEN 'Groupon (KFC)'
  WHEN inv.warehouse_id = 'GNNV' THEN 'Genco'
  WHEN inv.warehouse_id = 'GNCA' THEN 'Genco Canada (ON)'
  END AS fp,
CASE WHEN inv.Fulfillable_less_open_orders < 0 THEN 0 ELSE inv.Fulfillable_less_open_orders END AS Fulfillable_less_open_orders2,
inv.Unit_Cost * Fulfillable_less_open_orders2 AS fulfillable_less_open_cogs,
CASE WHEN inv.permalink IS NULL THEN pm.permalink  ||  inv.warehouse_id
  ELSE inv.permalink  ||  inv.warehouse_id
  END AS permalink_grouping,
sfd.opp_id_18,
sfd.md_id_18,
sfd.unit_sell_price,
CASE
     WHEN sfd.feature_date > CURRENT_DATE THEN 's'
  WHEN sfd.feature_date = CURRENT_DATE THEN 'y'
   WHEN live.live_deal IS NOT NULL THEN 'y'
  ELSE 'n'
  END AS deal_status,
CASE
  WHEN cp.id = 193 THEN 'y' ELSE 'n'
  END AS is_clearance2,
sfd.feature_date,
sfd.deal_end_date,
CASE
  WHEN mer.opp_id IS NOT NULL THEN 'TRUE'
  ELSE 'FALSE'
  END AS sku_conflict,
mer.opp_id AS sku_conflict_opp_id,
sfd.option_status

FROM sandbox.inv_snapshot inv
LEFT JOIN ( 
 SELECT * FROM (
 SELECT sfmulti.product_sku AS sku, 
 sfmulti.deal_option_key,
 sfopp.feature_date,
 CASE WHEN sfopp.groupon_expiration_date IS NOT NULL THEN  sfopp.groupon_expiration_date
   ELSE sfopp.feature_end_date
   END AS deal_end_date,
 sfmulti.opportunity_key,
 sfmulti.opportunity_id AS opp_id_18,
 sfmulti.multi_deal_id AS md_id_18,
 sfopp.permalink,
 sfopp.fulfillment_method,
 sfopp.fulfillment_provider,
 sfopp.opportunity_id,
 sfmulti.unit_sell_price,
 sfopp.Grpn_Basics_Flg,
 dopt.status AS option_status
 FROM dw.v_dim_multi_deal sfmulti
 LEFT JOIN dw.v_dim_deal_option dopt ON sfmulti.deal_option_key = dopt.deal_option_key
 LEFT JOIN dw.v_dim_opportunity sfopp ON sfmulti.opportunity_key = sfopp.opportunity_key
 WHERE sfopp.feature_date >= CURRENT_DATE 
  AND sfopp.record_type_key = '751005' -- Selects only Goods deals
  AND sfmulti.product_sku IS NOT NULL
  AND sfopp.fulfillment_method  IN ( 'Owned Inventory','Post-deal purchase 3PL','Pre-deal purchase 3PL')
UNION
 SELECT sfmulti.product_sku AS sku, 
 sfmulti.deal_option_key,
 sfopp.feature_date,
 CASE WHEN sfopp.groupon_expiration_date IS NOT NULL THEN  sfopp.groupon_expiration_date
   ELSE sfopp.feature_end_date
   END AS deal_end_date,
 sfmulti.opportunity_key,
 sfmulti.opportunity_id AS opp_id_18,
 sfmulti.multi_deal_id AS md_id_18,
 sfopp.permalink,
 sfopp.fulfillment_method,
 sfopp.fulfillment_provider,
 sfopp.opportunity_id,
 sfmulti.unit_sell_price,
 sfopp.Grpn_Basics_Flg,
 dopt.status AS option_status
 FROM dw.v_dim_multi_deal sfmulti
 LEFT JOIN dw.v_dim_deal_option dopt ON sfmulti.deal_option_key = dopt.deal_option_key
 LEFT JOIN dw.v_dim_opportunity sfopp ON sfmulti.opportunity_key = sfopp.opportunity_key
 WHERE  sfopp.record_type_key = '751005'
  AND sfmulti.product_sku IS NOT NULL
  AND sfopp.fulfillment_method  IN ( 'Owned Inventory','Post-deal purchase 3PL','Pre-deal purchase 3PL')
  AND sfmulti.opportunity_key IN (
       SELECT dopp.opportunity_key
       FROM dw.v_agg_deal_status_day daystats
       JOIN dw.v_dim_opportunity dopp ON daystats.opportunity_key = dopp.opportunity_key AND daystats.day_key = CAST(CAST(CURRENT_DATE - 1 AS FORMAT 'YYYYMMDD') AS VARCHAR(8)))
       WHERE dopp.record_type_key = '751005' 
       AND daystats.overall_availability_flag = 1)
UNION
 SELECT sfmulti.product_sku AS sku, 
 sfmulti.deal_option_key,
 sfopp.feature_date,
 CASE WHEN sfopp.groupon_expiration_date IS NOT NULL THEN  sfopp.groupon_expiration_date
   ELSE sfopp.feature_end_date
   END AS deal_end_date,
 sfmulti.opportunity_key,
 sfmulti.opportunity_id AS opp_id_18,
 sfmulti.multi_deal_id AS md_id_18,
 sfopp.permalink,
 sfopp.fulfillment_method,
 sfopp.fulfillment_provider,
 sfopp.opportunity_id,
 sfmulti.unit_sell_price,
 sfopp.Grpn_Basics_Flg,
 dopt.status AS option_status
 FROM dw.v_dim_multi_deal sfmulti
 LEFT JOIN dw.v_dim_deal_option dopt ON sfmulti.deal_option_key = dopt.deal_option_key
 LEFT JOIN dw.v_dim_opportunity sfopp ON sfmulti.opportunity_key = sfopp.opportunity_key
 WHERE sfopp.feature_date < CURRENT_DATE 
   AND sfopp.record_type_key = '751005' -- Selects only Goods deals
   AND sfopp.fulfillment_method  IN ( 'Owned Inventory','Post-deal purchase 3PL','Pre-deal purchase 3PL')
   AND  sfmulti.product_sku  || sfopp.fulfillment_provider NOT IN (
     SELECT sfmulti.product_sku  || sfopp.fulfillment_provider
     FROM dw.v_dim_opportunity sfopp
     LEFT JOIN dw.v_dim_multi_deal sfmulti ON sfopp.opportunity_key = sfmulti.opportunity_key  
     JOIN dw.v_agg_deal_status_day daystats ON sfopp.opportunity_key = daystats.opportunity_key AND daystats.day_key = CAST(CAST(CURRENT_DATE - 1 AS FORMAT 'YYYYMMDD') AS VARCHAR(8)))
     WHERE daystats.overall_availability_flag = 1 
     AND sfopp.record_type_key = '751005' 
     AND sfopp.fulfillment_method  IN ( 'Owned Inventory','Post-deal purchase 3PL','Pre-deal purchase 3PL')))
                        QUALIFY RANK() OVER (PARTITION BY sfmulti.product_sku, sfopp.fulfillment_provider
                        ORDER BY sfopp.feature_date DESC,
                            sfmulti.opportunity_key DESC,
                            sfmulti.deal_option_key DESC) IN (1)
) AS sfd ON (inv.sku = sfd.sku AND inv.warehouse_id = 
 CASE
     WHEN sfd.fulfillment_provider = 'ADS (SC)' THEN 'ads'
     WHEN sfd.fulfillment_provider = 'Innotrac (OH)' THEN 'inoh'
     WHEN sfd.fulfillment_provider = 'Innotrac (NV)' THEN 'innv'
     WHEN sfd.fulfillment_provider = 'Innotrac (IL)' THEN 'inil'
     WHEN sfd.fulfillment_provider = 'Groupon (KFC)' THEN 'kfc'
     WHEN sfd.fulfillment_provider = 'Genco' THEN 'GNNV'
     WHEN sfd.fulfillment_provider = 'Genco Canada (ON)' THEN 'GNCA'
     END)

 LEFT JOIN(
    SELECT UNIQUE
     sfmulti.product_sku AS sku,
      sfopp.permalink,                
     FROM dw.v_dim_multi_deal sfmulti
     LEFT JOIN dw.v_dim_opportunity sfopp ON sfmulti.opportunity_key = sfopp.opportunity_key
                        QUALIFY RANK() OVER (PARTITION BY sfmulti.product_sku 
                        ORDER BY sfopp.feature_date DESC,
                        sfopp.created_date DESC) IN (1)
    WHERE  sfopp.record_type_key = '751005'       
    AND (sfopp.feature_date > (2008-07-01) OR  sfopp.created_date > (2008-07-01))
    AND sfmulti.product_sku IS NOT NULL
    AND sfopp.permalink IS NOT NULL
    AND  sfmulti.product_sku IN (
           SELECT inv2.sku FROM sandbox.inv_snapshot inv2 WHERE permalink IS NULL)
  ) AS pm
    ON inv.sku = pm.sku

LEFT JOIN ( 
SELECT dopp.fulfillment_provider,
dopp.feature_date,
CASE
  WHEN dopp.groupon_expiration_date IS NOT NULL THEN dopp.groupon_expiration_date
  ELSE dopp.feature_end_date 
  END AS deal_end_date,
CASE
  WHEN dopp.feature_date > CURRENT_DATE THEN 'Upcoming'
  WHEN dopp.feature_date <= CURRENT_DATE THEN 'Live'
  ELSE 'Not Live'
  END AS deal_status,
dopp.permalink,
dmulti.opportunity_id AS opp_id,
dmulti.product_sku AS sku
FROM dw.v_dim_opportunity dopp
LEFT JOIN dw.v_dim_deal ddeal
ON dopp.opportunity_key = ddeal.opportunity_key
LEFT JOIN dw.v_dim_multi_deal dmulti
ON dopp.opportunity_key = dmulti.opportunity_key
LEFT JOIN dw.v_dim_deal_option dopt
ON dmulti.deal_option_key = dopt.deal_option_key
                        QUALIFY RANK() OVER (PARTITION BY dmulti.product_sku
                        ORDER BY dopp.feature_date DESC,
                            dmulti.opportunity_key DESC,
                            dmulti.deal_option_key DESC) IN (1)
WHERE dopp.record_type_key = '751005' -- Selects only Goods deals
AND dopp.fulfillment_method LIKE ANY ('%Owned Inventory%','%3PL%')
AND dopp.permalink NOT LIKE ALL ('%rosetta%')
AND  dopt.max_pledges > pledge_sum
AND dopt.status <> 'inactive' -- Not inactive in DE
AND  (dopp.feature_date >= CURRENT_DATE
  OR dopp.opportunity_key IN (
    SELECT dopp.opportunity_key
    FROM dw.v_agg_deal_status_day daystats
    JOIN dw.v_dim_opportunity dopp ON daystats.opportunity_key = dopp.opportunity_key AND daystats.day_key = CAST(CAST(CURRENT_DATE - 1 AS FORMAT 'YYYYMMDD') AS VARCHAR(8)))
    WHERE dopp.record_type_key = '751005' 
    AND daystats.overall_availability_flag = 1))
) AS mer
ON inv.sku = mer.sku AND 
CASE
  WHEN inv.warehouse_id = 'ads' THEN 'ADS (SC)'
  WHEN inv.warehouse_id = 'inil' THEN 'Innotrac (IL)'
  WHEN inv.warehouse_id = 'inoh' THEN 'Innotrac (OH)'
  WHEN inv.warehouse_id = 'innv' THEN 'Innotrac (NV)'
  WHEN inv.warehouse_id = 'kfc' THEN 'Groupon (KFC)'
  WHEN inv.warehouse_id = 'GNNV' THEN 'Genco'
  WHEN inv.warehouse_id = 'GNCA' THEN 'Genco Canada (ON)'
  END <> mer.fulfillment_provider
LEFT JOIN ( 
   SELECT dopp.opportunity_key AS live_deal
   FROM dw.v_agg_deal_status_day daystats
   JOIN dw.v_dim_opportunity dopp ON daystats.opportunity_key = dopp.opportunity_key AND daystats.day_key = CAST(CAST(CURRENT_DATE - 1 AS FORMAT 'YYYYMMDD') AS VARCHAR(8)))
   WHERE dopp.record_type_key = '751005' -- Selects only Goods deals
    AND daystats.overall_availability_flag = '1' -- Determines whether deal is active 
) AS live ON sfd.opportunity_key = live.live_deal
LEFT JOIN user_gp.campaigns c ON sfd.opportunity_id = c.opportunity_id (CASESPECIFIC)
LEFT JOIN user_gp.campaign_personas cp ON c.id = cp.campaign_id AND cp.id = 193

1 个答案:

答案 0 :(得分:0)

你必须先自己缩小范围。有多个UNION,所以首先运行查询,分别获取UNIONing数据集。这样它看起来不像是一个妈妈的任务。一旦找到具有多个WHERE子句的那个,就会开始拆分单个查询。我的猜测是你在某处缺少开括号或右括号。

即使看起来太难,也有一些简单的方法可以简化这一点......我建议你这样做。