[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

UNION query Teradata - error

Posted on 2016-07-26
2
Medium Priority
?
89 Views
Last Modified: 2016-07-26
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
Comment
Question by:Euro5
2 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 41730084
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
 

Author Closing Comment

by:Euro5
ID: 41730102
Excellent, thanks very much!!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exchange database can often fail to mount thereby halting the work of all users connected to it. Finding out why database isn’t mounting is crucial and getting the server back online. Stellar Phoenix Mailbox Exchange Recovery is a champion product t…
This article will show a step by step guide on how to mask column values in Oracle 12c using DBMS_REDACT full redaction option. This option is available on licensed Oracle Enterprise edition as part of Oracle's Advanced Security.
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question