Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- sql랭킹
- sql업데이트
- 인젝션
- sql순위
- VarType
- 이미지세로길이
- ERD
- injection
- join
- javascript 한글입력체크
- MSSQL보안
- jdbc driver
- array
- update
- wap
- WML
- FileSystemObject
- VARIABLE
- 한글입력체크
- XML
- 자바기초
- inner join
- xmldom
- 이미지가로길이
- instr
- asp함수
- 정규식
- tempDB
- JavaScript
- SPLIT
Archives
- Today
- Total
3초기억력
MS-SQL SubString으로 할수있는 연령대별 회원분석 쿼리문 본문
SELECT age_name, sex_name, SUM(total_count) AS total_count
FROM
(
SELECT
CASE WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 0 AND 20 THEN '20'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 21 AND 25 THEN '25'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 26 AND 30 THEN '30'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 31 AND 35 THEN '35'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 36 AND 40 THEN '40'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 41 AND 45 THEN '45'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 46 AND 50 THEN '50'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 51 AND 55 THEN '55'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 56 AND 60 THEN '60'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 >= 61 THEN '61'
END AS age_name, SUBSTRING(jumin, 7, 1) as sex_name, COUNT(num) AS total_count
FROM 회원테이블명
GROUP BY CAST(SUBSTRING(CAST(CONVERT(varchar(10), GETDATE(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + SUBSTRING(jumin, 1, 2) AS int) + 1, SUBSTRING(jumin, 7, 1)
) AS aa
GROUP BY age_name, sex_name
ORDER BY age_name
칼럼명 설명
- 주민번호 : jumin
- 성별 : sex_name (1:남자,2:여자)
- 나이대 : age_name (20:20대미만, 25:21살~25살, 30:26살~30살...)
- 회원수 : total_count
asp 페이지에서
머 이런식으로 사용하면 됨.
참고로 각 수치옆에 (%)로 해주면 더 좋겠죠?
asp 단에서 처리할 내용이 많을듯.
FROM
(
SELECT
CASE WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 0 AND 20 THEN '20'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 21 AND 25 THEN '25'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 26 AND 30 THEN '30'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 31 AND 35 THEN '35'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 36 AND 40 THEN '40'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 41 AND 45 THEN '45'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 46 AND 50 THEN '50'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 51 AND 55 THEN '55'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 BETWEEN 56 AND 60 THEN '60'
WHEN CAST(substring(CAST(CONVERT(varchar(10), getdate(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + substring(jumin, 1, 2) AS int) + 1 >= 61 THEN '61'
END AS age_name, SUBSTRING(jumin, 7, 1) as sex_name, COUNT(num) AS total_count
FROM 회원테이블명
GROUP BY CAST(SUBSTRING(CAST(CONVERT(varchar(10), GETDATE(), 121) AS varchar(10)), 1, 4) AS int)
- CAST('19' + SUBSTRING(jumin, 1, 2) AS int) + 1, SUBSTRING(jumin, 7, 1)
) AS aa
GROUP BY age_name, sex_name
ORDER BY age_name
칼럼명 설명
- 주민번호 : jumin
- 성별 : sex_name (1:남자,2:여자)
- 나이대 : age_name (20:20대미만, 25:21살~25살, 30:26살~30살...)
- 회원수 : total_count
asp 페이지에서
연령대 | 성별합 | 남자 | 여자 |
20살미만 | 100,000 | 50,000 | 50,000 |
21살~25살 | |||
26살~30살 | |||
31살~35살 | |||
.... | |||
61세이상 |
머 이런식으로 사용하면 됨.
참고로 각 수치옆에 (%)로 해주면 더 좋겠죠?
asp 단에서 처리할 내용이 많을듯.
'쿼리_MSSQL' 카테고리의 다른 글
응용 1 - Rank, 랭킹 뽑기 (0) | 2008.11.14 |
---|---|
MS-SQL UPDATE문을 제대로 사용하자! 2탄 (0) | 2008.11.13 |
ms-sql UPDATE 문을 제대로 써보자! (0) | 2008.11.13 |
MS-SQL 랭킹(순위) 매기기 (0) | 2008.11.13 |
ms-sql 2000 SQL인젝션방지용 쿼리문 (0) | 2008.11.13 |
Comments