월별, 년별 통계쿼리를 하다가 막혀서 질문드립니다.
category 테이블
cateID | CateName | depth | DisplaySeq | parentid |
1 | 서울지점 | 1 | 1 | 0 |
2 | 인천지점 | 1 | 2 | 0 |
3 | 안양지점 | 1 | 3 | 0 |
4 | 수원지점 | 1 | 4 | 0 |
5 | 대전지점 | 1 | 5 | 0 |
6 | 장소1 | 2 | 1 | 1 |
7 | 장소2 | 2 | 2 | 1 |
8 | 장소3 | 2 | 3 | 1 |
9 | 장소4 | 2 | 4 | 1 |
10 | 장소5 | 2 | 5 | 1 |
11 | 장소1 | 2 | 1 | 2 |
12 | 장소2 | 2 | 2 | 2 |
13 | 장소3 | 2 | 3 | 2 |
14 | 장소4 | 2 | 4 | 2 |
15 | 장소5 | 2 | 5 | 2 |
16 | 장소6 | 2 | 6 | 2 |
17 | 장소7 | 2 | 7 | 2 |
18 | 장소1 | 2 | 1 | 4 |
19 | 장소2 | 2 | 2 | 4 |
20 | 장소3 | 2 | 3 | 4 |
21 | 장소4 | 2 | 4 | 4 |
55 | 장소6 | 2 | 6 | 1 |
56 | 장소1 | 2 | 1 | 36 |
57 | 장소2 | 2 | 2 | 36 |
31 | 장소 | 2 | 1 | 3 |
32 | 장소 | 2 | 2 | 3 |
33 | 장소 | 2 | 3 | 3 |
34 | 장소 | 2 | 4 | 3 |
35 | 충주지점 | 1 | 6 | 0 |
36 | 마산지점 | 1 | 8 | 0 |
37 | 평촌지점 | 1 | 9 | 0 |
38 | 전남지점 | 1 | 10 | 0 |
39 | 광주지점 | 1 | 11 | 0 |
40 | 나주지점 | 1 | 12 | 0 |
41 | 부산지점 | 1 | 13 | 0 |
42 | 장소1 | 2 | 1 | 5 |
43 | 장소2 | 2 | 6 | 5 |
44 | 장소3 | 2 | 5 | 5 |
45 | 장소4 | 2 | 4 | 5 |
46 | 장소5 | 2 | 3 | 5 |
47 | 장소6 | 2 | 2 | 5 |
48 | 장소1 | 2 | 1 | 35 |
49 | 장소2 | 2 | 2 | 35 |
50 | 장소3 | 2 | 3 | 35 |
51 | 장소4 | 2 | 4 | 35 |
52 | 장소5 | 2 | 5 | 35 |
53 | 장소6 | 2 | 6 | 35 |
54 | 장소7 | 2 | 7 | 35 |
58 | 장소3 | 2 | 3 | 36 |
Sales 테이블
sidx | cateid | reportdate | Aban | Bban | Cban | Dban | Eban | parentid |
1 | 6 | 20160118 | 1 | 0 | 1 | 0 | 1 | 1 |
2 | 7 | 20160118 | 1 | 1 | 0 | 0 | 1 | 1 |
3 | 8 | 20160118 | 1 | 0 | 0 | 1 | 0 | 1 |
4 | 9 | 20160118 | 1 | 1 | 1 | 1 | 0 | 1 |
5 | 10 | 20160118 | 1 | 0 | 1 | 0 | 0 | 1 |
6 | 6 | 20160119 | 1 | 1 | 0 | 0 | 1 | 1 |
7 | 7 | 20160119 | 3 | 0 | 0 | 0 | 1 | 1 |
8 | 8 | 20160119 | 3 | 0 | 1 | 0 | 1 | 1 |
9 | 9 | 20160119 | 3 | 0 | 0 | 0 | 1 | 1 |
10 | 10 | 20160119 | 1 | 1 | 1 | 0 | 0 | 1 |
152 | 6 | 20160127 | 1 | 1 | 1 | 1 | 1 | 1 |
153 | 7 | 20160127 | 1 | 0 | 2 | 0 | 0 | 1 |
154 | 8 | 20160127 | 2 | 0 | 2 | 0 | 0 | 1 |
155 | 9 | 20160127 | 2 | 0 | 2 | 0 | 0 | 1 |
156 | 10 | 20160127 | 2 | 0 | 0 | 0 | 0 | 1 |
157 | 22 | 20160127 | 1 | 0 | 0 | 0 | 0 | 1 |
158 | 11 | 20160127 | 0 | 0 | 0 | 0 | 0 | 2 |
159 | 12 | 20160127 | 0 | 0 | 0 | 0 | 0 | 2 |
160 | 13 | 20160127 | 0 | 0 | 0 | 0 | 0 | 2 |
161 | 14 | 20160127 | 0 | 0 | 0 | 0 | 0 | 2 |
162 | 15 | 20160127 | 0 | 0 | 0 | 0 | 0 | 2 |
163 | 16 | 20160127 | 0 | 0 | 0 | 0 | 0 | 2 |
164 | 17 | 20160127 | 0 | 0 | 0 | 0 | 0 | 2 |
165 | 31 | 20160127 | 1 | 1 | 0 | 0 | 0 | 3 |
166 | 32 | 20160127 | 2 | 0 | 1 | 0 | 0 | 3 |
167 | 33 | 20160127 | 2 | 1 | 0 | 1 | 1 | 3 |
168 | 34 | 20160127 | 2 | 0 | 1 | 0 | 0 | 3 |
169 | 42 | 20160127 | 0 | 0 | 0 | 0 | 0 | 5 |
170 | 47 | 20160127 | 0 | 0 | 0 | 0 | 0 | 5 |
171 | 46 | 20160127 | 0 | 0 | 0 | 0 | 0 | 5 |
172 | 45 | 20160127 | 0 | 0 | 0 | 0 | 0 | 5 |
173 | 44 | 20160127 | 0 | 0 | 0 | 0 | 0 | 5 |
174 | 43 | 20160127 | 0 | 0 | 0 | 0 | 0 | 5 |
198 | 31 | 20160128 | 1 | 0 | 0 | 0 | 0 | 3 |
199 | 32 | 20160128 | 1 | 0 | 0 | 2 | 2 | 3 |
200 | 33 | 20160128 | 1 | 0 | 2 | 0 | 0 | 3 |
201 | 34 | 20160128 | 1 | 0 | 2 | 2 | 2 | 3 |
202 | 11 | 20160128 | 0 | 0 | 0 | 0 | 0 | 2 |
203 | 12 | 20160128 | 0 | 0 | 0 | 0 | 0 | 2 |
204 | 13 | 20160128 | 0 | 0 | 0 | 0 | 0 | 2 |
205 | 14 | 20160128 | 0 | 0 | 0 | 0 | 0 | 2 |
206 | 15 | 20160128 | 0 | 0 | 0 | 0 | 0 | 2 |
207 | 16 | 20160128 | 0 | 0 | 0 | 0 | 0 | 2 |
208 | 17 | 20160128 | 0 | 0 | 0 | 0 | 0 | 2 |
209 | 48 | 20160128 | 10 | 0 | 0 | 0 | 0 | 35 |
210 | 49 | 20160128 | 0 | 0 | 0 | 0 | 0 | 35 |
211 | 50 | 20160128 | 0 | 0 | 30 | 0 | 0 | 35 |
212 | 51 | 20160128 | 0 | 0 | 0 | 0 | 0 | 35 |
213 | 52 | 20160128 | 0 | 38 | 0 | 0 | 0 | 35 |
214 | 53 | 20160128 | 0 | 0 | 26 | 0 | 35 | 35 |
215 | 54 | 20160128 | 0 | 0 | 0 | 0 | 0 | 35 |
216 | 42 | 20160128 | 0 | 0 | 0 | 0 | 0 | 5 |
217 | 47 | 20160128 | 0 | 0 | 0 | 0 | 0 | 5 |
218 | 46 | 20160128 | 0 | 0 | 0 | 0 | 0 | 5 |
219 | 45 | 20160128 | 0 | 0 | 0 | 0 | 0 | 5 |
220 | 44 | 20160128 | 0 | 0 | 0 | 0 | 0 | 5 |
221 | 43 | 20160128 | 0 | 0 | 0 | 0 | 0 | 5 |
222 | 6 | 20160128 | 10 | 20 | 30 | 40 | 50 | 1 |
223 | 7 | 20160128 | 1 | 0 | 0 | 0 | 0 | 1 |
224 | 8 | 20160128 | 1 | 0 | 0 | 0 | 0 | 1 |
225 | 9 | 20160128 | 5 | 5 | 5 | 5 | 5 | 1 |
226 | 10 | 20160128 | 1 | 0 | 0 | 0 | 0 | 1 |
227 | 22 | 20160128 | 1 | 0 | 0 | 0 | 0 | 1 |
228 | 18 | 20160128 | 1 | 0 | 0 | 0 | 0 | 4 |
229 | 19 | 20160128 | 1 | 0 | 0 | 0 | 0 | 4 |
230 | 20 | 20160128 | 2 | 0 | 0 | 0 | 0 | 4 |
231 | 21 | 20160128 | 1 | 0 | 0 | 0 | 0 | 4 |
232 | 6 | 20150118 | 1 | 0 | 1 | 0 | 1 | 1 |
233 | 7 | 20150118 | 1 | 1 | 0 | 0 | 1 | 1 |
234 | 8 | 20150118 | 1 | 0 | 0 | 1 | 0 | 1 |
235 | 9 | 20150118 | 1 | 1 | 1 | 1 | 0 | 1 |
236 | 10 | 20150118 | 1 | 0 | 1 | 0 | 0 | 1 |
237 | 6 | 20150119 | 1 | 1 | 0 | 0 | 1 | 1 |
238 | 7 | 20150119 | 3 | 0 | 0 | 0 | 1 | 1 |
239 | 8 | 20150119 | 3 | 0 | 1 | 0 | 1 | 1 |
240 | 9 | 20150119 | 3 | 0 | 0 | 0 | 1 | 1 |
241 | 10 | 20150119 | 1 | 1 | 1 | 0 | 0 | 1 |
242 | 11 | 20150127 | 0 | 0 | 0 | 0 | 0 | 2 |
243 | 12 | 20150127 | 0 | 0 | 0 | 0 | 0 | 2 |
244 | 13 | 20150127 | 0 | 0 | 0 | 0 | 0 | 2 |
245 | 14 | 20150127 | 0 | 0 | 0 | 0 | 0 | 2 |
246 | 15 | 20150127 | 0 | 0 | 0 | 0 | 0 | 2 |
247 | 16 | 20150127 | 0 | 0 | 0 | 0 | 0 | 2 |
248 | 17 | 20150127 | 0 | 0 | 0 | 0 | 0 | 2 |
249 | 31 | 20150127 | 1 | 1 | 0 | 0 | 0 | 3 |
250 | 32 | 20150127 | 2 | 0 | 1 | 0 | 0 | 3 |
251 | 33 | 20150127 | 2 | 1 | 0 | 1 | 1 | 3 |
252 | 34 | 20150127 | 2 | 0 | 1 | 0 | 0 | 3 |
위 테이블을 참조해서 => 아래 테이블의 sum(Aban), sum(Bban), sum(Cban), sum(Dban) , sum(Eban) 까지 각각 합산
쿼리를 얻고 싶어서
select sum(Aban), sum(Bban), sum(Cban), sum(Dban), sum(Eban)
from Sales
where parentid=1 and cateid=6 and reportdate between 20160101 and 20160131
union all
select sum(Aban), sum(Bban), sum(Cban), sum(Dban), sum(Eban)
from Sales
where parentid=1 and cateid=7 and reportdate between 20160101 and 20160131
union all
select sum(Aban), sum(Bban), sum(Cban), sum(Dban), sum(Eban)
from Sales
where parentid=1 and cateid=8 and reportdate between 20160101 and 20160131
union all
select sum(Aban), sum(Bban), sum(Cban), sum(Dban), sum(Eban)
from Sales
where parentid=1 and cateid=9 and reportdate between 20160101 and 20160131
union all
select sum(Aban), sum(Bban), sum(Cban), sum(Dban), sum(Eban)
from Sales
where parentid=1 and cateid=10 and reportdate between 20160101 and 20160131
union all
select sum(Aban), sum(Bban), sum(Cban), sum(Dban), sum(Eban)
from Sales
where parentid=1 and cateid=22 and reportdate between 20160101 and 20160131
했는데 이게 날짜 별로 cateid 항목이 변동이 되거든요.
예를 들어서 표에 보시면 parentid=1인것이 20160127일자에는 cateid =22 가 새로 생성 되어 있어요.
어떻게 해야 할지 조언좀 부탁합니다.
1) 결과 값 : 현재날짜 기준 20160101 ~ 20160126 까지 현재날짜 : 20160126
cateid | Aban | Bban | Cban | Dban | Eban | parentid |
6 | 13 | 22 | 32 | 41 | 53 | 1 |
7 | 6 | 1 | 2 | 0 | 2 | 2 |
8 | 7 | 0 | 3 | 1 | 1 | 1 |
9 | 11 | 6 | 8 | 6 | 6 | 1 |
10 | 5 | 1 | 2 | 0 | 0 | 1 |
1) 결과 값 : 현재날짜 기준 20160101 ~ 20160131 까지
cateid | Aban | Bban | Cban | Dban | Eban | parentid |
6 | 13 | 22 | 32 | 41 | 53 | 1 |
7 | 6 | 1 | 2 | 0 | 2 | 2 |
8 | 7 | 0 | 3 | 1 | 1 | 1 |
9 | 11 | 6 | 8 | 6 | 6 | 1 |
10 | 5 | 1 | 2 | 0 | 0 | 1 |
22 | 2 | 0 | 0 | 0 | 0 | 1 |
2) 결과값 : 년별 20160101 ~ 20161231
Comment 4
-
건우아빠
2016.01.29 22:57
-
다자녀아빠
2016.01.29 23:52
헐 ...... 대..박!!
제가 너무 어렵게 생각한건가요?
정말 간단하게 끝내시네요.^^
건우 아빠님 매번 감사드려요.^^
혹시 가로 세로 total 값을 얻으려고
가로 total 값
select sum(Aban) Aban, sum(Bban) Bban, sum(Cban) Cban, sum(Dban) Dban, sum(Eban) Eban, total=sum(Aban+Bban+Cban+Dban+Eban)
from Sales
where parentid = 1 and reportdate between 20160101 and 20160131했는데 세로 total값은 어떻게 해야 하는지??
cateid Aban Bban Cban Dban Eban parentid total 6 13 22 32 41 53 1 161 7 6 1 2 0 2 2 11 8 7 0 3 1 1 1 12 9 11 6 8 6 6 1 37 10 5 1 2 0 0 1 8 22 2 0 0 0 0 1 2 total 44 30 47 48 62 -
건우아빠
2016.01.30 11:46
그룹 잡힌 컬럼이 하나 일때는 with rollup 쓰시면 간단하지만
둘 이상일 경우는 제어 하기가 어렵씁니다.
이경우는 GROUPING SETS 을 이용하시면 원하는 소계를 구현할수 있습니다.
-
다자녀아빠
2016.02.02 20:38
혹시 위의 테이블 참조해서 아래와 같은 결과를 얻으려면 어떻게 쿼리를 해야 하는지요?잘 되지가 않네요 ㅠㅠ
얻고싶은 결과 표1 또는 표2 표1 구분 1 2 3 4 5 6 7 합계 금년매출 13 22 35 32 42 65 53 262 작년매출 6 25 35 80 65 56 48 315 증감율 116.7 -12.0 0.0 -60.0 -35.4 16.1 10.4 35.77 금년인원 11 6 0 8 6 7 6 37 작년인원 5 1 1 2 25 55 15 45 증감율 120.0 500.0 -100.0 300.0 -76.0 -87.3 -60.0 596.7 표2 cateName 금년매출 작년매출 증감율 금년인원 작년인원 증감율 합계 서울지점 30 13 130.8 49 0 0.0 인천지점 176581 145789 21.1 347 0 0.0 안양지점 379 33 1048.5 472 0 0.0 수원지점 10 0 0.0 121 0 0.0 대전지점 9 0 0.0 273 0 0.0 충주지점 0 0 0.0 10 0 0.0 마산지점 139 0 0.0 15 0 0.0 표 2 쿼리를 위해 아래와 같이 만들었는데 증감율이 오류가 나오네요('0'으로 나눌수없다는..??)
select a.CateName ,
nowyear
,oldyear as oldyear
,(nowyear- oldyear)/oldyear as '비율'
,c.cur_mcount as cur_mcount
,d.old_mcount as old_mcountfrom
(
/* --- 금년매출------*/
select c.CateName as CateName, isnull(sum(Aban+bban+cban+dban+eban),0) as nowyear
from Sales s right outer join category c
on s.parentid=c.cateid
where left(s.reportdate,4)=2016
group by c.CateName, c.DisplaySeq
) a
full join(
/* --- 전년매출------*/
select CateName, sum(Aban+bban+cban+dban+eban) as oldyear
from Sales ss right outer join category cc
on ss.parentid=cc.cateid
where left(ss.reportdate,4)=2015
group by cc.CateName
) b
on a.CateName=b.CateNamefull join
(
/* --- 금년인원수------*/
select ccc.CateName, isnull(sum(m.mancount),0) as cur_mcount
FROM category ccc join ManCount m
on ccc.cateid=m.cateid
where ccc.cateid=m.cateid and left(reportdate,4) = 2016
group by ccc.CateName
) con a.CateName=c.CateName
full join
(
/* --- 전년인원수------*/
select ccc.CateName, isnull(sum(m.mancount),0) as old_mcount
FROM category ccc join ManCount m
on ccc.cateid=m.cateid
where ccc.cateid=m.cateid and left(reportdate,4) = 2015
group by ccc.CateName
) don a.CateName=d.CateName
select cateid , sum(Aban) Aban, sum(Bban) Bban, sum(Cban) Cban, sum(Dban) Dban, sum(Eban) Eban
from Sales
where parentid = 1 and reportdate between 20160101 and 20160131
group by cateid