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
운영게시판
최근게시물
MS-SQL Q&A 3598 게시물 읽기
No. 3598
쿼리문 질문합니다....도와주세요...
작성자
지화복(hwabok)
작성일
2007-07-01 19:11
조회수
2,529


안녕하세요...?


table1


column1   column2   column3       일자

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

aa             1                  1          2007-06-01

aa             1                  2          2007-06-01

aa             1                  3          2007-06-01

aa             2                  1          2007-06-01

aa             2                  2          2007-06-01

aa             2                  3          2007-06-01

aa             2                  4          2007-06-21

aa             3                  1          2007-06-21

aa             3                  2          2007-06-21




위와 같은 데이터가 있을때


얻고자흐는 값은


일자          column1  column2갯수     column3갯수

2007-06-01    aa           2                       6

2007-06-21    aa           2                       3


이렇습니다....


어떻게 쿼리를 하여야 하는지요....?


 

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

조금 헷갈리지만

select A.tdate, tcol1, tcol2, tcol3
from
(
select distinct(col1) tcol1, tdate
from TA
group by col1, tdate
) A
left join
(
select count(col2) tcol2, tdate
from
(select distinct(col2), tdate
from TA
group by col2, tdate
) TB
group by tdate
) B
on A.tdate=B.tdate
left join
(
select count(col3) tcol3, tdate
from TA
group by tdate
) C
on A.tdate=C.tdate

이렇게 하니 되네요. 테이블 이름을 TA로 하고 컬럼은 각각 col1, col2, col3, tdate로 했습니다.
다른 컬럼이름은 전부 임시로 만든 것들입니다.

아무개님이 2007-07-01 21:37에 작성한 댓글입니다. Edit
create table test(column1 char(2),column2 int,column3 int,일자 char(10))
 
insert test
select 'aa',1,1,'2007-06-01' union all
select 'aa',1,2,'2007-06-01' union all
select 'aa',1,3,'2007-06-01' union all
select 'aa',2,1,'2007-06-01' union all
select 'aa',2,2,'2007-06-01' union all
select 'aa',2,3,'2007-06-01' union all
select 'aa',2,4,'2007-06-21' union all
select 'aa',3,1,'2007-06-21' union all
select 'aa',3,2,'2007-06-21'
 
-------------------------------------------------------
select일자,
       column1,
       count(distinct column2) as column2갯수,
       count(distinct str(column2)+str(column3)) as column3갯수
from test
group by 일자,column1
 
/*
일자        column1 column2갯수 column3갯수
---------- ------- ----------- -----------
2007-06-01 aa      2           6
2007-06-21 aa      2           3
 
(2개행적용됨)
*/
최석준(beatchoi)님이 2007-07-03 09:53에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
3601테이블 설계 조언을 부탁합니다. [1]
이상철
2007-07-03
2432
3600insert 시에 이진 데이터 잘립니다 할때 잘려도 억지로 insert 되게.. [3]
초보
2007-07-03
8335
3599균등 SELECT
장대서
2007-07-03
2240
3598쿼리문 질문합니다....도와주세요... [2]
지화복
2007-07-01
2529
3597그날이 속한 주의 첫째날과 마지막 날 구하는 방법 [2]
황경운
2007-06-30
4194
3596ms sql에서는 최대로 만들수있는 필드수가... [2]
김태원
2007-06-29
2341
3595자료형에 관한 질문드립니다.
이석
2007-06-29
2386
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.017초, 이곳 서비스는
	PostgreSQL v16.4로 자료를 관리합니다