Solved

UNION query Teradata - error

Posted on 2016-07-26
2
45 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 73

Accepted Solution

by:
sdstuber earned 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Pl/SQL Query 31 61
Data center mess 4 46
Database connection opened on a machine 8 29
Updating ms sql with special characters 8 20
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now