• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 90
  • Last Modified:

UNION query Teradata - error

Getting error on this UNION query. Can anyone help?

 2.JPG

SELECT 

natl_acct_nbr,
szip as "Shipper Zip",
rzip as "Recipient Zip",
zippair as "Ziip Pair",
shp_cntry as "Origin City",
recp_cntry_cd as "Destination City",
last_known_srt_loc_scn_loc_cd,
ilane as "Intl Lane",
count(natl_acct_nbr)

FROM ( 


Select 
a.natl_acct_nbr,
v.last_known_srt_loc_scn_loc_cd,
''  as szip,
'' as rzip,
shp_cntry,
recp_cntry_cd,
'' as zippair,
shp_cntry||'  '||recp_cntry_cd as ilane



From  
((((UI_ISH_PROD_DB.billed_intl_express i Inner Join
UI_ISH_PROD_DB.billed_intl_misc im On i.inv_unique_id= im.inv_unique_id
            And    i.AB_SEQ_NBR = im.ab_seq_nbr) Left Outer Join 
ui_ish_prod_db.billed_intl_misc_resn_cd ic On im.misc_rsn_cd = ic.misc_resn_cd) Inner Join
SCAN_PROD_RESTRICTED_VIEW_DB.exprs_vol_vsbty_esqi_enhmt v ON i.SHP_TRK_NBR = v.shp_trk_nbr)
Inner Join UI_ISH_PROD_DB.customer a ON  i. payr_cust_nbr  = a.cust_nbr)

            Where  
             a.cust_nbr = i.payr_cust_nbr
            AND  inv_month between 201601 and 201606
            --And    e.ab_inv_typ_ind not in ('0')
            And im.misc_rsn_cd IN ('273', '357')
            AND v.last_known_srt_loc_scn_loc_cd IN ('CANH', 'CGNH')

           
 UNION ALL
 
 Select 
a.natl_acct_nbr,
v.last_known_srt_loc_scn_loc_cd,
LEFT(e.ZONE_ORIG_PSTL_CD,5) as szip,
LEFT(e.RECP_PSTL_CD,5) as rzip,
LEFT(e.ZONE_ORIG_PSTL_CD,5)||'- '||LEFT(e.RECP_PSTL_CD,5) AS zippair,
' ' As shp_cntry,
' ' As recp_cntry_cd,
' 'As ilane


From  
((((UI_ISH_PROD_DB.billed_dom_express e Inner Join
UI_ISH_PROD_DB.dom_inv_misc dm On e.INV_UNIQUE_ID = dm.inv_unique_id
And    e.AB_SEQ_NBR = dm.ab_seq_nbr) Left Outer Join 
ui_ish_prod_db.miscellaneous_reason_code rc On dm.misc_resn_cd = rc.misc_resn_cd) Inner Join
SCAN_PROD_RESTRICTED_VIEW_DB.exprs_vol_vsbty_esqi_enhmt v ON e.SHP_TRK_NBR = v.shp_trk_nbr) 
Inner Join UI_ISH_PROD_DB.customer a ON  e.payr_cust_nbr  = a.cust_nbr)


            Where  
             a.cust_nbr = e.payr_cust_nbr
            AND  inv_dt_yyyymm between 201601 and 201606
            And    e.ab_inv_typ_ind not in ('0')
            And dm.misc_resn_cd = '050'
            AND v.last_known_srt_loc_scn_loc_cd = 'INDH'
            
 ) temp           
 

Open in new window

2.JPG
0
Euro5
Asked:
Euro5
1 Solution
 
sdstuberCommented:
you need a GROUP BY clause on your outer query

something like

  SELECT natl_acct_nbr,
         szip        AS "Shipper Zip",
         rzip        AS "Recipient Zip",
         zippair     AS "Ziip Pair",
         shp_cntry   AS "Origin City",
         recp_cntry_cd AS "Destination City",
         last_known_srt_loc_scn_loc_cd,
         ilane       AS "Intl Lane",
         COUNT(natl_acct_nbr)
    FROM (SELECT a.natl_acct_nbr,
                 v.last_known_srt_loc_scn_loc_cd,
                 ''                               AS szip,
                 ''                               AS rzip,
                 shp_cntry,
                 recp_cntry_cd,
                 ''                               AS zippair,
                 shp_cntry || '  ' || recp_cntry_cd AS ilane
            FROM ((((ui_ish_prod_db.billed_intl_express i
                     INNER JOIN ui_ish_prod_db.billed_intl_misc im
                         ON i.inv_unique_id = im.inv_unique_id AND i.ab_seq_nbr = im.ab_seq_nbr)
                    LEFT OUTER JOIN ui_ish_prod_db.billed_intl_misc_resn_cd ic
                        ON im.misc_rsn_cd = ic.misc_resn_cd)
                   INNER JOIN scan_prod_restricted_view_db.exprs_vol_vsbty_esqi_enhmt v
                       ON i.shp_trk_nbr = v.shp_trk_nbr)
                  INNER JOIN ui_ish_prod_db.customer a ON i.payr_cust_nbr = a.cust_nbr)
           WHERE     a.cust_nbr = i.payr_cust_nbr
                 AND inv_month BETWEEN 201601 AND 201606
                 --And    e.ab_inv_typ_ind not in ('0')
                 AND im.misc_rsn_cd IN ('273', '357')
                 AND v.last_known_srt_loc_scn_loc_cd IN ('CANH', 'CGNH')
          UNION ALL
          SELECT a.natl_acct_nbr,
                 v.last_known_srt_loc_scn_loc_cd,
                 left(e.zone_orig_pstl_cd, 5)                                  AS szip,
                 left(e.recp_pstl_cd, 5)                                       AS rzip,
                 left(e.zone_orig_pstl_cd, 5) || '- ' || left(e.recp_pstl_cd, 5) AS zippair,
                 ' '                                                           AS shp_cntry,
                 ' '                                                           AS recp_cntry_cd,
                 ' '                                                           AS ilane
            FROM ((((ui_ish_prod_db.billed_dom_express e
                     INNER JOIN ui_ish_prod_db.dom_inv_misc dm
                         ON e.inv_unique_id = dm.inv_unique_id AND e.ab_seq_nbr = dm.ab_seq_nbr)
                    LEFT OUTER JOIN ui_ish_prod_db.miscellaneous_reason_code rc
                        ON dm.misc_resn_cd = rc.misc_resn_cd)
                   INNER JOIN scan_prod_restricted_view_db.exprs_vol_vsbty_esqi_enhmt v
                       ON e.shp_trk_nbr = v.shp_trk_nbr)
                  INNER JOIN ui_ish_prod_db.customer a ON e.payr_cust_nbr = a.cust_nbr)
           WHERE     a.cust_nbr = e.payr_cust_nbr
                 AND inv_dt_yyyymm BETWEEN 201601 AND 201606
                 AND e.ab_inv_typ_ind NOT IN ('0')
                 AND dm.misc_resn_cd = '050'
                 AND v.last_known_srt_loc_scn_loc_cd = 'INDH') temp
GROUP BY natl_acct_nbr,
         szip,
         rzip,
         zippair,
         shp_cntry,
         recp_cntry_cd,
         last_known_srt_loc_scn_loc_cd,
         ilane

Open in new window

0
 
Euro5Author Commented:
Excellent, thanks very much!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now