the problem here is that the first half of the output is NOT returning the data for WWDTA but for sure there are matches. All we are doing in the second part of this query is taking the sales rep id and getting the sales rep name for display in the report.

CREATE VIEW astccdta.acwocmpk AS (                             
SELECT                                                         
  ALL       T01.OHORD#, T01.OHSLR#,T01.OHORDT, T01.OHORDD,     
T01.OHTTN$,  '                    ' as WWDTA                   
  FROM      ASTDTA.OEORHDOH  T01,                              
            ASTDTA.OETRANOT  T02                               
  WHERE     T01.OHORD# = T02.OTORD#                            
    AND(    T02.OTTRNC = 'WOC')                                
and T01.OHORDD > 20120101                                      
UNION ALL                                                      
SELECT                                                         
  ALL       T01.OHORD#, T01.OHSLR#, T01.OHORDT, T01.OHORDD,    
T01.OHTTN$,                                                    
            SUBSTR(RFDTA,1,20) AS WWDTA                        
  FROM      ASTCCDTA.WOCREPS T01,                              
            ASTCCDTA.REPREF1 T02                               
   WHERE     T01.OHSLR# = T02.RFSLC)   
有帮助吗?

解决方案 2

What UNION does is to take two separate SELECT statements and combine them in one result set, one after the other. Say your first SELECT brings back:

1 A 2013-08-01 100.00 ''
2 B 2013-08-02 200.00 ''
3 A 2013-08-03 300.00 ''

and your second SELECT brings back:

1 A 2013-08-01 100.00 'John Smith'
2 B 2013-08-02 200.00 'Jane Jones'
3 A 2013-08-03 300.00 'John Smith'

When you UNION them you get:

1 A 2013-08-01 100.00 ''
2 B 2013-08-02 200.00 ''
3 A 2013-08-03 300.00 ''
1 A 2013-08-01 100.00 'John Smith'
2 B 2013-08-02 200.00 'Jane Jones'
3 A 2013-08-03 300.00 'John Smith'

I think that you want to alter the first SELECT to JOIN to the sales rep name table and drop the UNION and second SELECT altogether:

SELECT ALL 
  T01.OHORD#, T01.OHSLR#,T01.OHORDT, T01.OHORDD, T01.OHTTN$, SUBSTR(RFDTA,1,20) AS WWDTA
FROM ASTDTA.OEORHDOH T01,
     ASTDTA.OETRANOT T02,
     ASTCCDTA.REPREF1 T03
WHERE T01.OHORD# = T02.OTORD#
 AND (T02.OTTRNC = 'WOC')
 and  T01.OHORDD > 20120101
 and  T01.OHSLR# = T03.RFSLC

其他提示

The problem is the 2nd SELECT has an extra column at the end.

The clue the system gave you was that the number of columns was inconsistent, between the two sides of the UNION.

Add an extra, empty char(20) column at the end of the first SELECT list to match it, then you should be OK there. But check that each column in the first SELECT matches the corresponding column in the 2nd, and that they appear in the same order.

As others pointed out, you probably want UNION ALL.

In MS SQL Server you can use the union keyword

SELECT                                                                        
  ALL       T01.OHORD#, T01.OHORDT, T01.OHORDD, T01.OHSLR#, T01.OHTTN$        
  FROM      ASTDTA/OEORH1 T01,                                                
            ASTCCDTA/OETRA99 T02                                              
  WHERE     T01.OHORD# = T02.OTORD#                                           
    AND(    T02.OTTRNC = 'WOC')      

UNION

SELECT                                                                              
  ALL       T01.OHORD#, T01.OHORDT, T01.OHORDD, T01.OHSLR#, T01.OHTTN$        
            SUBSTR(RFDTA,1,20) AS WWDTA                                             
  FROM      PKLIB/WOCREPS T01,                                                      
            PKLIB/PHILREF1 T02                                                      
  WHERE     T01.OHSLR# = T02.RFSLC   

If your data will contain duplicates use Union, otherwise UNION ALL is more efficient (it doesn't try to dedupe your results)

As Ganders has pointed out this needs the columns to match, this is very difficult to provide an answer for without knowing your schema.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top