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 5828 게시물 읽기
No. 5828
월이 주어 졌을때 주간별 통계내기 질문입니다.
작성자
한호(hantamu)
작성일
2011-01-19 16:22
조회수
8,192

안녕하세요 새로 쿼리를 시작한 초보의 질문입니다.
도움을 주신다면 열심히 자라서 나중에 더 큰 도움을 주는 사람으로 찾아 뵙겠습니다. ^^

통계를 처음 해보게 되었습니다.

년도를 선택하면 월별로 통계를 내는 쿼리는 검색을 해서 찾아서 잘 풀었는데요

월을 선택하면 주간별로 통계를 내는것은 생각보다 많이 어렵네요

월별은 01월 부터 12월 까지 고정이라 쉽게 되었던거 같은데

주간별은 달마다 달라서 쉽지가 않습니다. ㅜ.ㅜ

2011년만 보더라도 1월달은 6주가 있고 2월은 5주고 ...

제가 원하는 건 다음과 같습니다.

특정 월을 선택하면 가로로 주간 통계 치가 나오는 것 입니다.

테이블이 다음과 같이 있을때

CREATE TABLE InOut(
 [cdate] [datetime] NOT NULL,
 [Input] [int] NOT NULL
)
각 주간별로 input 이 1인 레코드 개수와 주간별 총 개수를 가로로 보고 싶습니다.

예를 들면

2011년 01월 주간별 통계

SELECT .......
  FROM InOut
 WHERE year(cdate) = 2011 and month(cdate) = 01


비고   1주   2주   3주   4주   5주   6주   총합
===============================================
인풋    3    19    24     8     0     0     54
전체    4    19    25    10     0     0     58
===============================================
총 4명이 input 하지 않음

이런식으로 나오는 통계를 작성하고 싶습니다.

     


 

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

with t as (
select cast('20110101' as datetime) cdate, 1 Input union all
select cast('20110102' as datetime) cdate, 9 Input union all
select cast('20110103' as datetime) cdate, 3 Input union all
select cast('20110104' as datetime) cdate, 2 Input union all
select cast('20110105' as datetime) cdate, 0 Input union all
select cast('20110106' as datetime) cdate, 4 Input union all
select cast('20110107' as datetime) cdate, 5 Input union all
select cast('20110108' as datetime) cdate, 2 Input union all
select cast('20110109' as datetime) cdate, 1 Input union all
select cast('20110110' as datetime) cdate, 0 Input union all
select cast('20110111' as datetime) cdate, 1 Input union all
select cast('20110112' as datetime) cdate, 4 Input union all
select cast('20110113' as datetime) cdate, 5 Input union all
select cast('20110114' as datetime) cdate, 8 Input union all
select cast('20110115' as datetime) cdate, 4 Input union all
select cast('20110116' as datetime) cdate, 7 Input union all
select cast('20110117' as datetime) cdate, 2 Input union all
select cast('20110118' as datetime) cdate, 1 Input union all
select cast('20110119' as datetime) cdate, 6 Input union all
select cast('20110120' as datetime) cdate, 9 Input union all
select cast('20110121' as datetime) cdate, 0 Input union all
select cast('20110122' as datetime) cdate, 4 Input union all
select cast('20110123' as datetime) cdate, 6 Input union all
select cast('20110124' as datetime) cdate, 7 Input union all
select cast('20110125' as datetime) cdate, 3 Input union all
select cast('20110126' as datetime) cdate, 8 Input union all
select cast('20110127' as datetime) cdate, 9 Input union all
select cast('20110128' as datetime) cdate, 2 Input union all
select cast('20110129' as datetime) cdate, 5 Input union all
select cast('20110130' as datetime) cdate, 1 Input union all
select cast('20110131' as datetime) cdate, 8 Input
)
select isnull(cast(year(cdate) as varchar(4)) +'.'+ cast(month(cdate) as varchar(2)), 'TOTAL') '비고'
     , sum(case when datename(week, cdate) = 1 then Input end) '1주'
     , sum(case when datename(week, cdate) = 2 then Input end) '2주'
     , sum(case when datename(week, cdate) = 3 then Input end) '3주'
     , sum(case when datename(week, cdate) = 4 then Input end) '4주'
     , sum(case when datename(week, cdate) = 5 then Input end) '5주'
     , sum(case when datename(week, cdate) = 6 then Input end) '6주'
  from t
 where year(cdate)  = 2011
   and month(cdate) = 01
 group by cast(year(cdate) as varchar(4)) +'.'+ cast(month(cdate) as varchar(2)) with rollup

知音(sunnylee72)님이 2011-01-20 15:07에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
5832합계테이블 관련 업데이트 알고리즘 질문
정신없이
2011-01-26
5564
5830그룹에 그룹의 카운트?? [1]
김동욱
2011-01-25
5736
5829update 쿼리 질문드립니다.. ㅠㅠ [1]
바보
2011-01-21
5927
5828월이 주어 졌을때 주간별 통계내기 질문입니다. [1]
한호
2011-01-19
8192
5827프로시져인데 당췌 왜 에러가 나는지 고수님들 부탁드려요
소울보더
2011-01-19
6797
5825간단한 쿼리 질문입니다. [2]
성승제
2011-01-18
6226
5823서버컴퓨터에서 C드라이브 용량문제입니다. [2]
정명규
2011-01-14
7062
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.024초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다