#title 그룹별 순번 만들기 [[TableOfContents]] ==== 개요 ==== 그룹별 순번 만들기는 아래와 같은 각각의 type에 대해서 순번을 매기는 것을 말한다. {{{ seq type title ----------- ------------ -------------------------------------------------------------------------------- 1 business Straight Talk About Computers 2 business The Busy Executive's Database Guide 3 business Cooking with Computers: Surreptitious Balance Sheets 4 business You Can Combat Computer Stress! 1 mod_cook The Gourmet Microwave 2 mod_cook Silicon Valley Gastronomic Treats 1 popular_comp Secrets of Silicon Valley 2 popular_comp Net Etiquette 3 popular_comp But Is It User Friendly? 1 psychology Emotional Security: A New Algorithm 2 psychology Computer Phobic AND Non-Phobic Individuals: Behavior Variations 3 psychology Is Anger the Enemy? 4 psychology Life Without Fear 5 psychology Prolonged Data Deprivation: Four Case Studies 1 trad_cook Sushi, Anyone? 2 trad_cook Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean 3 trad_cook Fifty Years in Buckingham Palace Kitchens 1 UNDECIDED The Psychology of Computer Cooking }}} ==== SQL Server 2000 버전 ==== {{{ SELECT title, type, IDENTITY(INT, 1, 1) seq INTO #temp FROM titles SELECT seq%cnt + 1 seq, a.type, a.title FROM #temp a INNER JOIN ( SELECT MIN(type) type , COUNT(*) cnt FROM titles GROUP BY type) b ON a.type = b.type ORDER BY a.type, seq%cnt + 1 DROP TABLE #temp --임시테이블은 순번을 만들기 위해서 쓴 것이다.. --역시 오라클의 rownum이 없는 것이 참 아쉽다. 임시테이블을 만들지 않고서도 순번을 만들 수는 있으나 --순번을 매기는 것은 임시테이블을 사용하는 것이 부하가 더 적다. 다음의 쿼리와 I/O를 비교해 보면 알 것이다. --2005버전은 Row_Number()함수를 이용하면 된다. SELECT COUNT(*) seq, a.title_id, a.type FROM titles a INNER JOIN titles b ON a.type = b.type AND a.title_id >= b.title_id GROUP BY a.title_id, a.type }}} ==== SQL Server 2005 이상 버전 ==== 2005 이상 버전에서는 [Windowing Functiion의 이해]를 참고하라.