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 1301 게시물 읽기
No. 1301
분기별로 통계내기 쿼리 질문요
작성자
석훈
작성일
2004-04-25 10:56ⓒ
2004-04-25 10:57ⓜ
조회수
4,578

결제테이블에서

상품,가격,결제일자

select sum(가격) from 상품테이블

이걸 상품별, 분기별로 조회하고싶습니다

결제일자를 분기별로 쿼리작성하려면 어떻게해야되나요?

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

pubs db에서 테스트 한 겁니다.

이것과 유사한 방식으로 하면 되지 않을까요.

날짜를 데이터타입에 따라 약간 달라질 수 있겠지만..

 

아래쪽은 연도가 주어졌을때 상점,분기별계를

크로스텝형태로 나열해 본 것입니다.

 

select a.stor_id,
       a.yyyy,
       a.quater,      
       isnull(sum(a.qty), 0)
from (
     select a.stor_id,
            a.yyyy,
            case when a.mm between  1 and  3 then '1분기'
                 when a.mm between  4 and  6 then '2분기'
                 when a.mm between  7 and  9 then '3분기'
                 when a.mm between 10 and 12 then '4분기'
            end  quater,      
            sum(a.qty)      qty 
     from (
          select stor_id         stor_id,
                 year(ord_date)  yyyy,
                 month(ord_date) mm,
                 sum(qty)        qty
          from sales
          group by stor_id,
                   year(ord_date),
                   month(ord_date)
          ) a
     group by a.stor_id, a.yyyy, a.mm
     ) a
group by a.stor_id, a.yyyy, a.quater
order by a.stor_id, a.yyyy, a.quater

 

 

 

select isnull(a.stor_id, '분기계') '상점',
       isnull(sum(case a.quater when '1분기' then a.qty end), 0) '1분기',
       isnull(sum(case a.quater when '2분기' then a.qty end), 0) '2분기',
       isnull(sum(case a.quater when '3분기' then a.qty end), 0) '3분기',
       isnull(sum(case a.quater when '4분기' then a.qty end), 0) '4분기',
       isnull(sum(a.qty), 0)                                     '상점계'
from (
      select a.stor_id,
             case when a.mm between  1 and  3 then '1분기'
                  when a.mm between  4 and  6 then '2분기'
                  when a.mm between  7 and  9 then '3분기'
                  when a.mm between 10 and 12 then '4분기'
             end  quater,      
             sum(a.qty)      qty 
      from (
           select stor_id         stor_id,
                  month(ord_date) mm,
                  sum(qty)        qty
           from sales
           where ord_date between cast('1993-01-01' as datetime) and cast('1993-12-31' as datetime)
           group by stor_id,
                    year(ord_date),
                    month(ord_date)
           ) a
      group by a.stor_id, a.mm
      ) a
group by a.stor_id with rollup
order by isnull(a.stor_id, '총계')

mssql님이 2004-04-25 11:52에 작성한 댓글입니다.
이 댓글은 2004-04-25 12:19에 마지막으로 수정되었습니다. Edit
[Top]
No.
제목
작성자
작성일
조회
1304두개의 테이블에서 합계내기(통계) [1]
뚜웅
2004-04-29
4632
1303저한테만 어려운거 ㅡㅜ [1]
초보
2004-04-27
2479
1302왕초보)칼럼에 primary 키하고 foreign key를 주고 싶습니다. [2]
정인호
2004-04-27
3099
1301분기별로 통계내기 쿼리 질문요 [1]
석훈
2004-04-25
4578
1300많은 테이블 수 vs 많은 필드 수? [1]
김무길
2004-04-23
2626
1298[질문]@@identity 값은 4byte까지만 처리가능한가요?
나왕이야
2004-04-22
2829
1297테이블 생성전에 테이블이 존재하는지 알려면.. [2]
과니
2004-04-22
3315
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.018초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다