Postgresql:语法错误

时间:2017-05-04 15:59:45

标签: sql postgresql postgresql-8.3

如何正确添加单位的聚合列(sprd_units)? 它应该通过mrkt_desc,offer_seq_yr_nr,offer_seq_nr_desc和sprd_nr对单位求和。我收到错误:“子查询必须只返回一列” 我究竟做错了什么? 这是查询。

SELECT     
    d.mrkt_desc_txt, 
    d.offr_seq_cmpgn_yr_nr, 
    d.brchr_qtr_nr, 
    d.offr_seq_nr_desc,
    d.offr_cmpgn_nr, 
    d.veh_desc_txt, 
    d.sls_cls_desc_txt, 
    CASE WHEN d.veh_desc_txt='CORE BROCHURE' THEN d.sprd_nr ELSE cb.sprd_cb END AS sprd_nr,
    (SELECT 
        d.mrkt_desc_txt,
        d.offr_seq_cmpgn_yr_nr,
        d.offr_seq_nr_desc,
        d.sprd_nr,
        sum(d.unit_qty) AS sprd_units
    FROM ext_ce.gsam_v_data_detail d
    WHERE d.veh_desc_txt::text = 'CORE BROCHURE'::text OR d.veh_desc_txt::text = 'GENERAL REPRESENTATIVES FLYER'::text
    GROUP BY 
        d.mrkt_desc_txt,
        d.offr_seq_cmpgn_yr_nr,
        d.offr_seq_nr_desc,
        d.sprd_nr) as sprd_units
    d.sub_clstr_desc_txt,
    c.prod_typ_desc_txt, 
    c.bus_desc_txt, 
    c.ce_new_cat, 
    c.ce_new_cat_short, 
    c.catgry_desc_txt, 
    c.sgmt_desc_txt, 
    c.form_grp_desc_txt, 
    c.form_desc_txt,
    c.brnd_desc_txt, 
    c.brnd_postnng_fr_desc_txt, 
    c.gendr_desc_txt,
    vc.wrkhrs_grp,
    vc.brnd_fmly_desc_txt_adj,
    round(sum(CASE WHEN d.ver_desc_txt = 'FINAL LEADER LIST' THEN d.profit_amt/f.exch_rate ELSE 0 END)::numeric, 2) AS dp_dol_FLL, 
    round(sum(CASE WHEN d.ver_desc_txt = 'BEST OFFICIAL PROJECTION' THEN d.profit_amt/f.exch_rate ELSE 0 END)::numeric, 2) AS dp_dol_BOP, 
    round(sum(CASE WHEN d.ver_desc_txt = 'FINAL LEADER LIST' THEN d.spcl_prc_grs_sls_amt::double precision / f.exch_rate ELSE 0 END)::numeric, 2) AS gr_sales_dol_FLL,
    round(sum(CASE WHEN d.ver_desc_txt = 'BEST OFFICIAL PROJECTION' THEN d.spcl_prc_grs_sls_amt::double precision / f.exch_rate ELSE 0 END)::numeric, 2) AS gr_sales_dol_BOP,
    sum(CASE WHEN d.ver_desc_txt = 'FINAL LEADER LIST' THEN d.unit_qty ELSE 0 END) AS units_FLL,
    sum(CASE WHEN d.ver_desc_txt = 'BEST OFFICIAL PROJECTION' THEN d.unit_qty ELSE 0 END) AS units_BOP
    FROM       
    ext_ce.gsam_v_data_detail as d
    JOIN ext_ce.gsam_v_data_context as c
        ON (c.prmry_prfl_cd::numeric = d.prmry_prfl_cd)
    JOIN ext_ce.info_fx_rates_v2 as f
        ON ( d.mrkt_desc_txt = f.mrkt_desc_txt)
    LEFT JOIN (SELECT DISTINCT g.prmry_prfl_cd, g.wrkhrs_grp, g.brnd_fmly_desc_txt_adj FROM ext_ce.gsam_v_data_context AS g) AS vc
        ON vc.prmry_prfl_cd = d.prmry_prfl_cd
    LEFT JOIN (SELECT d.mrkt_desc_txt, d.offr_seq_cmpgn_yr_nr, d.offr_seq_nr_desc, d.prmry_prfl_cd, min(d.sprd_nr) AS sprd_cb
    FROM ext_ce.gsam_v_data_detail d
    WHERE d.veh_desc_txt::text = 'CORE BROCHURE'::text
    GROUP BY d.mrkt_desc_txt, d.offr_seq_cmpgn_yr_nr, d.offr_seq_nr_desc, d.prmry_prfl_cd) cb ON d.mrkt_desc_txt::text = cb.mrkt_desc_txt::text AND d.offr_seq_cmpgn_yr_nr = cb.offr_seq_cmpgn_yr_nr AND d.offr_seq_nr_desc = cb.offr_seq_nr_desc AND d.prmry_prfl_cd = cb.prmry_prfl_cd
    WHERE      
    c.prod_typ_desc_txt <> 'SALES AIDS'::text 
    AND d.cnsmr_invstmt_bdgt_desc_txt::text <> 'Sampling'::text 
    AND c.prmry_prfl_cd::numeric = d.prmry_prfl_cd
    AND f.offr_cmpgn_yr_nr = 2017
    AND d.offr_seq_cmpgn_yr_nr > 2014
    AND d.mrkt_desc_txt = f.mrkt_desc_txt
    GROUP BY   
    d.mrkt_desc_txt, 
    d.offr_seq_cmpgn_yr_nr, 
    d.brchr_qtr_nr, 
    d.offr_seq_nr_desc,
    d.offr_cmpgn_nr, 
    d.veh_desc_txt, 
    d.sls_cls_desc_txt, 
    d.sprd_nr,
    bs.sprd_units,
    d.sub_clstr_desc_txt,
    c.prod_typ_desc_txt, 
    c.bus_desc_txt, 
    c.ce_new_cat, 
    c.ce_new_cat_short, 
    c.catgry_desc_txt, 
    c.sgmt_desc_txt, 
    c.form_grp_desc_txt, 
    c.form_desc_txt,
    c.brnd_desc_txt, 
    c.brnd_postnng_fr_desc_txt, 
    c.gendr_desc_txt,
    vc.wrkhrs_grp,
    vc.brnd_fmly_desc_txt_adj,
    cb.sprd_cb;

我忘了添加。我使用Postgresql 8.3

0 个答案:

没有答案