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 928 게시물 읽기
No. 928
에러가 나는데 좀 잡아주세요...
작성자
이성훈(lsh0039)
작성일
2006-02-21 13:28
조회수
7,685

select A.sa_ctcode,A.ct_ltd, sum(A.sa_2003), sum(A.sa_2004), sum(A.sa_2005) from

(select sa_ctcode, ct_ltd, sum(sa_chamt) as sa_2003, 0 as sa_2004, 0 as sa_2005
from salelib.saletrs left outer join sujulib.custmst on sa_ctcode = ct_code
where sa_hisa='11' and sa_date between '20030101' and '20030131' group by sa_ctcode,ct_ltd

union

select sa_ctcode, ct_ltd, 0 as sa_2003, sum(sa_chamt) as sa_2004, 0 as sa_2005
from salelib.saletrs left outer join sujulib.custmst on sa_ctcode = ct_code
where sa_hisa='11' and sa_date between '20040101' and '20040131' group by sa_ctcode,ct_ltd

union

select sa_ctcode, ct_ltd, 0 as sa_2003, 0 as sa_2004, sum(sa_chamt) as sa_2005
from salelib.saletrs left outer join sujulib.custmst on sa_ctcode = ct_code
where sa_hisa='11' and sa_date between '20050101' and '20050131' group by sa_ctcode,ct_ltd
) A

group by A.sa_ctcode, A.ct_ltd

 

 

*구문상 머가 잘못되었는가요? 서브쿼리 입니다.

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

sqlcode나  sqlstate가 무엇인지 알수있나요?

도님이 2006-02-21 13:57에 작성한 댓글입니다. Edit

 

Error: SQL0199 - Keyword UNION not expected. Valid tokens: ). (State:37000, Native Code: FFFFFF39)
입니다.

안에 서브쿼리는 잘 실행되는데... 밖에 group by 한게 에러나는데요

이성훈(lsh0039)님이 2006-02-21 13:59에 작성한 댓글입니다.
이 댓글은 2006-02-21 13:59에 마지막으로 수정되었습니다.

db2에서는 union 과 서브쿼리시 상위select에서 group by 를 사용할수 없습니다.

아래처럼 사용하시면 될껍니다

=====================================================

select a.sa_ctcode, a.ct_ltd,
       sum(a.sa_2003), sum(a.sa_2004), sum(a.sa_2005)
from
(
select sa_ctcode, ct_ltd, sa_date
  case
  when substr(sa_date,1,4)  = '2003'
  then sum(sa_chamt)
  else 0
  end as sa_2003,
  case
  when substr(sa_date,1,4)  = '2004'
  then sum(sa_chamt)
  else 0
  end as sa_2004,
  case
  when substr(sa_date,1,4)  = '2005'
  then sum(sa_chamt)
  else 0
  end as sa_2005
from salelib.saletrs
where sa_hisa = '11'
and substr(sa_date,1,6) in ('200301','200401','200501')
group by sa_ctcode, ct_ltd, sa_date
)a
group by a.sa_ctcode, a.ct_ltd
====================================================

 

outer조인은 추가로 넣어주시면 될껍니다

최양일님이 2006-02-21 14:04에 작성한 댓글입니다.
이 댓글은 2006-02-21 14:30에 마지막으로 수정되었습니다. Edit

V7,V8에서 테스트했습니다.

 

select c1,count(*)
from (
select 1 c1,count(*) from org
group by DEPTNUMB
union all
select 2 c1,count(*) from org
group by DEPTNUMB
) a
group by c1;

 

C1          2         
----------- -----------
          1           8
          2           8

  2 record(s) selected.

 

 

도님이 2006-02-21 14:33에 작성한 댓글입니다. Edit

 그럼 위의 구문이 이상없다는건가요? 도님...?

 

이성훈(lsh0039)님이 2006-02-21 15:10에 작성한 댓글입니다.

db2 version이 어떻게 되나요?

도님이 2006-02-21 15:29에 작성한 댓글입니다. Edit

에러 코드에 대해 이런게 있네요

 

운영 체제:
  OS/400

 

소프트웨어 버전:
  V5R2M0; V5R3M0

 

Beginning with R520, a FULLSELECT on derived tables can now be done. In previous releases, trying this may have resulted in the following message: SQL0199 - Keyword UNION not expected. Valid tokens: ).

The FULLSELECT is a component of the select-statement and the CREATE VIEW statement. A FULLSELECT enclosed in parenthesis is called a subquery . A FULLSELECT specifies a result table. If UNION is not used, the result of the FULLSELECT is the result of the specified subselect. UNION derives a result table by combining two other result tables. The following is a simple example of how support for FULLSELECT of a derived table can work at R520:

select sum(derived.fld1) from (select fld1 from filea UNION select fld1 from fileb) as Derived                                                          
This is documented in the SQL Reference manual, in the chapter called Queries , found in the iSeries Information Center at the following Web site:

http://publib.boulder.ibm.com/html/as400/infocenter.html .

도님이 2006-02-21 17:35에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
931(3) db2 log? [1]
올아클
2006-02-25
5769
930(2)백업에 대해서 [3]
올아클
2006-02-23
6592
929[질문]실수형 데이타형은 무엇인가요? [2]
이준식
2006-02-23
9218
928에러가 나는데 좀 잡아주세요... [7]
이성훈
2006-02-21
7685
927테이블의 데이타를 못 불러 오네요.. 도와주세요~ [1]
DB2문맹
2006-02-20
5614
926Log full in archival Logging [1]
Hyung
2006-02-20
5189
925[질문]사용자 정의 테이블만 보고 싶어요. [5]
이준식
2006-02-16
6119
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.018초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다