sql 2008 r2
5만건 이하 테이블입니다. 아래와 같이 반복되는 날짜데이터 앞에 카테고리 번호를 부여하려고 합니다.
날짜별로 7개씩 반복되는데 cateid에 번호를 부여하는 쿼리를 어떻게 하는지? ㅠㅠ
select Row_NUMBER() over(order by a.reportdate desc) as RowNum,* from(
select '' as cateid,reportdate, reportyoil, aaa, bbb, ccc, ddd, eee, 2 as 'parentid'
from [dbo].Sales
where reportdate >= 20020401
group by reportdate,reportyoil, aaa, bbb, ccc, ddd, eee,
) a
order by RowNum desc
Sales 테이블
쿼리로 얻고 싶은 결과
---------------------------------
RowNum | cateid | reportdate | reportyoil | aaa | bbb | ccc | ddd | eee | parentid |
17132 | 11 | 20020402 | 화 | 331 | 2717259 | 760012 | 0 | 627278 | 2 |
17131 | 12 | 20020402 | 화 | 330 | 752784 | 999793 | 147835 | 0 | 2 |
17130 | 13 | 20020402 | 화 | 178 | 414852 | 437627 | 103467 | 0 | 2 |
17129 | 14 | 20020402 | 화 | 167 | 46374 | 362537 | 80461 | 0 | 2 |
17128 | 15 | 20020402 | 화 | 158 | 50734 | 346005 | 80287 | 0 | 2 |
17127 | 16 | 20020402 | 화 | 97 | 10912 | 216901 | 44645 | 0 | 2 |
17126 | 17 | 20020402 | 화 | 0 | 0 | 0 | 0 | 0 | 2 |
17125 | 11 | 20070224 | 토 | 264 | 3945038 | 0 | 360938 | 784086 | 2 |
17124 | 12 | 20070224 | 토 | 153 | 557274 | 54099 | 48640 | 302293 | 2 |
17123 | 13 | 20070224 | 토 | 131 | 99097 | 104110 | 54095 | 414130 | 2 |
17122 | 14 | 20070224 | 토 | 127 | 234556 | 110016 | 38185 | 350031 | 2 |
17121 | 15 | 20070224 | 토 | 84 | 157729 | 75922 | 29093 | 168651 | 2 |
17120 | 16 | 20070224 | 토 | 78 | 61823 | 66830 | 10000 | 345038 | 2 |
17119 | 17 | 20070224 | 토 | 63 | 2492530 | 0 | 970026 | 240003 | 2 |
17118 | 11 | 20070225 | 일 | 242 | 3474571 | 0 | 433671 | 628180 | 2 |
17117 | 12 | 20070225 | 일 | 159 | 493186 | 92742 | 65460 | 405926 | 2 |
17116 | 13 | 20070225 | 일 | 137 | 334566 | 101382 | 64095 | 285023 | 2 |
17115 | 14 | 20070225 | 일 | 98 | 90915 | 50465 | 32730 | 300939 | 2 |
17114 | 15 | 20070225 | 일 | 59 | 2825469 | 0 | 930928 | 276820 | 2 |
17113 | 16 | 20070225 | 일 | 52 | 189550 | 37733 | 27275 | 124096 | 2 |
17112 | 17 | 20070225 | 일 | 33 | 12728 | 15459 | 0 | 142285 | 2 |
17111 | 20070228 | 수 | 60 | 830007 | 0 | 119102 | 303666 | 2 | |
17110 | 20070228 | 수 | 50 | 166362 | 83648 | 42730 | 351848 | 2 | |
17109 | 20070228 | 수 | 46 | 32729 | 37736 | 15455 | 165918 | 2 | |
17108 | 20070228 | 수 | 41 | 50910 | 10455 | 0 | 165014 | 2 | |
17107 | 20070228 | 수 | 4 | 236362 | 0 | 18184 | 29546 | 2 | |
17106 | 20070228 | 수 | 0 | 0 | 0 | 0 | 0 | 2 | |
17105 | 20070301 | 목 | 0 | 4394936 | 0 | 366397 | 883178 | 2 | |
17104 | 20070301 | 목 | 0 | 1609110 | 0 | 322742 | 233182 | 2 | |
17103 | 20070301 | 목 | 0 | 294544 | 121842 | 62730 | 583215 | 2 | |
17102 | 20070301 | 목 | 0 | 148184 | 79556 | 15455 | 335941 | 2 | |
17101 | 20070301 | 목 | 0 | 146366 | 71376 | 43640 | 256840 | 2 | |
17100 | 20070301 | 목 | 0 | 128189 | 102291 | 15455 | 363674 | 2 | |
17099 | 20070301 | 목 | 0 | 38184 | 5911 | 20455 | 182287 | 2 |
Comment 3
-
Hisory
2016.02.04 22:44
-
다자녀아빠
2016.02.05 00:49
아..정말 감사합니다.
적용해서 응용하니 잘 되네요.
dense_rank() Over(Partition By reportyoil Order By RowNum) + 10 as cateid 시간되면 설명좀 부탁합니다.
실제 db에는 rownum값이 없어서 처음 실행할때는 rownum값이 잘못되었다고 에러가 나더라구요.
그레서 뷰로 만들어 넣고 실행하니 되긴되네요.
-
폭주
2016.02.05 15:53
https://msdn.microsoft.com/ko-kr/library/ms173825(v=sql.100).aspx
MSDN 참고 하시면 도움 되실듯 합니다.
도움이 되실지 모르겠네여...
Declare @tb table
(
RowNum varchar(100)
, reportdate varchar(100)
, reportyoil nvarchar(100)
, aaa varchar(100)
, bbb varchar(100)
, ccc varchar(100)
, ddd varchar(100)
, eee varchar(100)
, parentid varchar(100)
)
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17132','20020402',N'화','331','2717259','760012','0','627278','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17131','20020402',N'화','330','752784','999793','147835','0','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17130','20020402',N'화','178','414852','437627','103467','0','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17129','20020402',N'화','167','46374','362537','80461','0','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17128','20020402',N'화','158','50734','346005','80287','0','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17127','20020402',N'화','97','10912','216901','44645','0','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17126','20020402',N'화','0','0','0','0','0','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17125','20070224',N'토','264','3945038','0','360938','784086','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17124','20070224',N'토','153','557274','54099','48640','302293','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17123','20070224',N'토','131','99097','104110','54095','414130','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17122','20070224',N'토','127','234556','110016','38185','350031','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17121','20070224',N'토','84','157729','75922','29093','168651','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17120','20070224',N'토','78','61823','66830','10000','345038','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17119','20070224',N'토','63','2492530','0','970026','240003','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17118','20070225',N'일','242','3474571','0','433671','628180','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17117','20070225',N'일','159','493186','92742','65460','405926','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17116','20070225',N'일','137','334566','101382','64095','285023','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17115','20070225',N'일','98','90915','50465','32730','300939','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17114','20070225',N'일','59','2825469','0','930928','276820','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17113','20070225',N'일','52','189550','37733','27275','124096','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17112','20070225',N'일','33','12728','15459','0','142285','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17111','20070228',N'수','60','830007','0','119102','303666','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17110','20070228',N'수','50','166362','83648','42730','351848','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17109','20070228',N'수','46','32729','37736','15455','165918','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17108','20070228',N'수','41','50910','10455','0','165014','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17107','20070228',N'수','4','236362','0','18184','29546','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17106','20070228',N'수','0','0','0','0','0','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17105','20070301',N'목','0','4394936','0','366397','883178','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17104','20070301',N'목','0','1609110','0','322742','233182','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17103','20070301',N'목','0','294544','121842','62730','583215','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17102','20070301',N'목','0','148184','79556','15455','335941','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17101','20070301',N'목','0','146366','71376','43640','256840','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17100','20070301',N'목','0','128189','102291','15455','363674','2')
insert @tb ( RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid ) values('17099','20070301',N'목','0','38184','5911','20455','182287','2')
Select RowNum , reportdate , reportyoil , aaa , bbb , ccc , ddd , eee , parentid
, dense_rank() Over(Partition By reportyoil Order By RowNum) + 10 as cateid
From @tb