# 주단위, 월단위, 년단위, 분기별 단위 sql 통계 구현하기

2012. 7. 6. 16:59
반응형

-- 주 단위

Select DATEPART(ww, dates), count(order_no) From 테이블

group by DATEPART(ww, dates)

order by DATEPART(ww, dates)

-- 월단위

Select DATEPART(mm, dates), count(order_no) From tables

group by DATEPART(mm, dates)

order by DATEPART(mm, dates)

-- 년단위

Select DATEPART(yy, dates), count(order_no) From tables

group by DATEPART(yy, dates)

order by DATEPART(yy, dates)

--분기별

SELECT A.memyear,A.AA,SUM(A.CNT) AS CNT

FROM (

Select DATEPART(yy, dates) as memyear,DATEPART(mm, dates) as memmonth,  count(dates) as cnt

,(CASE DATEPART(mm, dates) WHEN '1' THEN '1' WHEN '2' THEN '1' WHEN '3' THEN '1'

WHEN '4' THEN '2' WHEN '5' THEN '2' WHEN '6' THEN '2'

WHEN '7' THEN '3' WHEN '8' THEN '3' WHEN '9' THEN '3'

WHEN '10' THEN '4'

WHEN '11' THEN '4'

WHEN '12' THEN '4' ELSE '0' END) AS AA

From Tables

group by DATEPART(mm, dates),DATEPART(yy, dates)

--order by DATEPART(ww, dates)

) A

group by A.memyear,A.AA

order by A.memyear,A.AA

반응형

#### '데이터베이스 > MS-SQL 😃' 카테고리의 다른 글

Microsoft SQL Server VS Oracle 데이터형식 맞추기  (0) 2012.11.20 2012.07.25 2012.07.03 2012.03.08 2011.12.01

비밀글모드

• 글쓰기
• 관리자
• 카테고리
• 맨위로