Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

UNION query Teradata - error

Posted on 2016-07-26
2
Medium Priority
?
88 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

782 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