database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
ㆍDB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
DB2 Q&A 1282 게시물 읽기
No. 1282
union all에 관해 문의 드립니다.
작성자
박진수(adue)
작성일
2007-03-22 12:04
조회수
7,097

각각의 쿼리는 조회가 되는데 union all 사용하면 Error: SQL0199 - 키워드 UNION가 예상한 키워드가 아님. 이라고 나옵니다.
뭘 잘못한걸까요..아시는분 있으시면 답변 부탁드립니다.



select * from (
select   distinct(a.wi31ai) as item, b.wa25ad as name, a.wi31ap as in_date, 
         a.wi31an as bl_no, (case a.wi31aj when 'N2' then '1' when 'N3' then '2' when 'N4' then '3' end) as revision,
         a.wi31ar as qty, a.wi31av as out_date, a.wi31at as inv, a.wi31au as con_qty,
--S-T Act 1
     coalesce(sum(case when c.ib12ae = 'JB24' then c.ib12bi end),0) as JB24,
        coalesce(sum(case when c.ib12ae = 'JB14' then c.ib12bi end),0) as JB14,
        coalesce(sum(case when c.ib12ae = 'JB13' then c.ib12bi end),0) as JB13,
        coalesce(sum(case when c.ib12ae = 'JB12' then c.ib12bi end),0) as JB12,       
        coalesce(sum(case when c.ib12ae = 'JB17' then c.ib12bi end),0) as JB17,
--Total(S-T Act 1)
    (coalesce(sum(case when c.ib12ae = 'JB24' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JB14' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JB13' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JB12' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JB17' then c.ib12bi end),0)) as act1,
--S-T Act 2
        coalesce(sum(case when c.ib12ae = 'JB11' then c.ib12bi end),0) as JB11,
        coalesce(sum(case when c.ib12ae = 'JD04' then c.ib12bi end),0) as JD04,
  '' as ct10,
        coalesce(sum(case when c.ib12ae = 'JC11' then c.ib12bi end),0) as JC11,
        coalesce(sum(case when c.ib12ae = 'JC12' then c.ib12bi end),0) as JC12,
        coalesce(sum(case when c.ib12ae = 'JC01' then c.ib12bi end),0) as JC01,
        coalesce(sum(case when c.ib12ae = 'JB25' then c.ib12bi end),0) as JB25,
--Total(S-T Act 2)
        coalesce(sum(case when c.ib12ae = 'JB11' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JD04' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC11' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC12' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC01' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JB25' then c.ib12bi end),0) as act2,
--vat(S-T Act 2)
 ((coalesce(sum(case when c.ib12ae = 'JB11' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JD04' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC11' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC12' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC01' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JB25' then c.ib12bi end),0)) / 10) as vat,
--Total Result
(coalesce(sum(case when c.ib12ae = 'JB11' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JD04' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC11' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC12' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC01' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JB25' then c.ib12bi end),0))+
((coalesce(sum(case when c.ib12ae = 'JB11' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JD04' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC11' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC12' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JC01' then c.ib12bi end),0) +
        coalesce(sum(case when c.ib12ae = 'JB25' then c.ib12bi end),0)) / 10) as total
        ,SUBSTR(CHAR(a.wi31bf),5,2) as end_date        
FROM     ldbcfs.twi31m a,
         ldbcfs.twa25m b,
         ldbcom.tib12m c
WHERE    a.wi31bf between 20070125 and 20070225
and      c.ib12aw='1208653367'
and      a.wi31ai = b.wa25ab
AND      a.wi31aa = c.ib12aa
AND      a.wi31ab = c.ib12ab
AND      a.wi31ac = c.ib12ac
and      a.wi31ad = c.ib12ad
and      a.wi31ae = c.ib12af
and      a.wi31an = c.ib12ag
and      a.wi31ao = c.ib12ah
and      a.wi31ak = c.ib12ai
and      a.wi31al = c.ib12aj
and      a.wi31am = c.ib12ak
group by b.wa25ad, a.wi31ap, 
         a.wi31an, a.wi31aj,
         a.wi31ar, a.wi31av, a.wi31at, a.wi31au, a.wi31ai, a.wi31bf
order by a.wi31an

UNION all

select   distinct(a.wi31ai), '' as name, a.wi31ap as in_date, 
         a.wi31an as bl_no, (case a.wi31aj when 'N2' then '1' when 'N3' then '2' when 'N4' then '3' end) as revision,
         a.wi31ar as qty, a.wi31av as out_date, a.wi31at as inv, a.wi31au as con_qty,
'' as JB24, '' as JB14, '' as JB13, '' as JB12, '' as JB17, '' as act1, '' as JB11,
'' as JD04, coalesce(sum(case when f.ut50af = 'CT10' then f.ut50ao end),0) as ct10,               
'' as JC11, '' as JC12, '' as JC01, '' as JB25, '' act2, '' as vat, '' as total,
SUBSTR(CHAR(a.wi31bf),5,2) as end_date    
from ldbcfs.twi31m a, ldbtrs.tum03m d, ldbtrs.tuc02t e, ldbtrs.tut50t f
where f.ut50ae > 0
AND   a.wi31bf between 20070125 and 20070225
and   a.wi31ak = d.um03aa
and   a.wi31al = d.um03ab
and   a.wi31am = d.um03ac
and   e.uc02aa = f.ut50aa
and   e.uc02ab = f.ut50ab
and   e.uc02ac = f.ut50ac
and   e.uc02ad = f.ut50ad
and   e.uc02ao = d.um03ad
and   d.um03ag = e.uc02ai
and   d.um03ah = e.uc02aj
group by a.wi31ap, a.wi31an, a.wi31aj,
         a.wi31ar, a.wi31av, a.wi31at, a.wi31au, a.wi31ai, a.wi31bf
order by a.wi31an )

이 글에 대한 댓글이 총 1건 있습니다.

union all 명령 사용시 각 퀄리의 컬럼의 수가 맞아야 합니다 .
첫번째 컬럼은 29개정도의 컬럼이 있는데 
두번째 컬럼은 26개입니다. 누락분이 있는것 같습니다.
수고하세요

구광모님이 2007-03-22 19:03에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
1285decimal데이터타입에 대해 물어보려구요. [1]
가영아
2007-03-23
7729
1284sql이 궁금해서 문의 드립니다. [4]
궁금해
2007-03-22
7128
1283db2diag.log 로테이션 [2]
백봉현
2007-03-22
6888
1282union all에 관해 문의 드립니다. [1]
박진수
2007-03-22
7097
1281인크리멘탈 백업 시 TRACKMOD on 의 의미? [1]
양금석
2007-03-21
6659
1280recover,rollforward시 OVERFLOW LOG PATH 에대해서 질문드립니다. [1]
db2초보
2007-03-21
6955
1279MPP 머신에서 db2 파티션 구성에 대한 질문 [4]
db2man
2007-03-20
7328
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.018초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다