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
운영게시판
최근게시물
Sybase Q&A 1276 게시물 읽기
No. 1276
쿼리문 좀 만들어 주세요... 좀 급합니다...
작성자
유석우(dreamf)
작성일
2005-09-04 19:33
조회수
4,238

create table B (
b_id char(12) not null,
b_flag int default 0 not null,
constraint pB primary key
clustered (b_id)
)

 

create table A (
a_id char(18) not null,
b_id char(12) not null,
a_title varchar(255) not null,
constraint pA primary key
clustered (a_id),
constraint fA foreign key (b_id)
references B (b_id)
)

 

위와 같이 테이블이 구성되어 있고 테이블에 데이터가

 

table B
b_id b_flag
('000000000001', 1)
('000000000002', 1)
('000000000003', 0)
('000000000004', 0)

 

table A
a_id b_id a_title
('000000000000000001', '000000000001', 'title1')
('000000000000000002', '000000000001', 'title1')
('000000000000000003', '000000000002', 'title2')
('000000000000000004', '000000000002', 'title2')
('000000000000000005', '000000000003', 'title3')
('000000000000000006', '000000000004', 'title4')
('000000000000000007', '000000000004', 'title5')

 

이렇게 있다고 할 때

뽑아내고자 하는 결과값은

 

a_id a_title
('000000000000000001', 'title1')
('000000000000000003', 'title2')
('000000000000000005', 'title3')
('000000000000000006', 'title4')
('000000000000000007', 'title5')

 

이렇습니다.

 

MySQL 에서는

 

select a.a_id, a.a_title from A a, B b where a.b_id = b.b_id and b.b_flag = 0
union
select a.a_id, a.a_title from A a, B b where a.b_id = b.b_id and b.b_flag = 1 group by b.b_id
order by a.a_id;

 

이렇게 하면 원하는 결과값을 뽑을 수 있는데, Sybase에서는 그렇지가 않네요...
방법 좀 가르쳐 주세요...

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

 

만약 이정도라면

select min (a_id), a_title
from A
group by a_title

해도 답이 될 것 같습니다.

김기환(kkh3333)님이 2005-09-05 14:07에 작성한 댓글입니다.

우선 답변 감사합니다... 만,

님이 댓글 다신 쿼리는

b_id 가 다르고 a_title 이 같은 데이터가 있을 경우 제대로 결과값이 나오질 않을 것 같네요...

 

table A
a_id b_id a_title
('000000000000000001', '000000000001', 'title1')
('000000000000000002', '000000000001', 'title1')
('000000000000000003', '000000000002', 'title2')
('000000000000000004', '000000000002', 'title2')
('000000000000000005', '000000000003', 'title3')
('000000000000000006', '000000000004', 'title4')
('000000000000000007', '000000000004', 'title5')

('000000000000000008', '000000000004', 'title1')

 

이럴 경우에 말이죠...

제가 원하는 결과값은,

 

a_id a_title
('000000000000000001', 'title1')
('000000000000000003', 'title2')
('000000000000000005', 'title3')
('000000000000000006', 'title4')
('000000000000000007', 'title5')

('000000000000000008', 'title1')

 

이렇게 나오는 거거든요... 제발 누가 쿼리 좀 만들어 주세요...

유석우(dreamf)님이 2005-09-05 14:36에 작성한 댓글입니다.
이 댓글은 2005-09-05 14:40에 마지막으로 수정되었습니다.

아래를 돌려보세요......

create table table_A(
a_id char(18),
b_id  char(12),
a_title char(10)
);
insert into table_A values ('000000000000000001', '000000000001', 'title1');
insert into table_A values ('000000000000000002', '000000000001', 'title1');
insert into table_A values ('000000000000000003', '000000000002', 'title2');
insert into table_A values ('000000000000000004', '000000000002', 'title2');
insert into table_A values ('000000000000000005', '000000000003', 'title3');
insert into table_A values ('000000000000000006', '000000000004', 'title4');
insert into table_A values ('000000000000000007', '000000000004', 'title5');
insert into table_A values ('000000000000000008', '000000000004', 'title1');
commit;


select b_id,a_title,
       min(a_id) a_id
  into #tmp1
  from table_A
group by b_id, a_title
;

select b_id,
    a_id,
    a_title,
       min(rowid(table_A)) rd
  into #tmp2
  from table_A
group by b_id, a_id,a_title
;


select a.a_id,
 a.a_title
  from table_A a,
       #tmp1 b,
       #tmp2 c
 where a.b_id = b.b_id
   and a.a_title = b.a_title
   and a.a_title = c.a_title
   and a.b_id = c.b_id
   and b.a_id = c.a_id
   and c.rd = rowid(a)
order by a.a_id
;

 

 

 
a_id a_title
('000000000000000001', 'title1')
('000000000000000003', 'title2')
('000000000000000005', 'title3')
('000000000000000006', 'title4')
('000000000000000007', 'title5')

('000000000000000008', 'title1')

 

보니까.. 뭘하실려는 지는 알겠습니다.

담부터.. 질문하실때는 사용 DB를 꼭 써주세요.. 위는 ASIQ기준으로 했습니다.

12.43버젼에서는 현재 한 쿼리로는 안됩니다.

놀놀이님이 2005-09-06 13:24에 작성한 댓글입니다. Edit

본인의 쿼리와 답변다신 분의 쿼리를 종합합니다...ㅋㅋ

 


select a.a_id, a.a_title from A a, B b where a.b_id = b.b_id and b.b_flag = 0
union
select min (a_id), a_title
from A
group by a_title

 

a_id               a_title
----               -------
000000000000000001 title1 
000000000000000003 title2 
000000000000000005 title3 
000000000000000006 title4 
000000000000000007 title5 
000000000000000008 title1 

수고하세요

 

 

 

지연님이 2005-09-06 13:25에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
1279ASE 12.5인데요. 윈도우 2003에서 설치가 안되나요 [1]
이일환
2005-09-07
3251
1278더미테이블 관련 질문 [1]
2005-09-06
4357
1277한글이요 [1]
2005-09-06
4979
1276쿼리문 좀 만들어 주세요... 좀 급합니다... [4]
유석우
2005-09-04
4238
1275IQ ODBC 연결 오류 [1]
김상익
2005-09-04
3337
1274transaction 관련하여 테스트한 결과 문의 드립니다. [2]
최유복
2005-09-02
4510
1273BCP에서 에러가... [3]
찡어
2005-09-02
3584
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.021초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다