코드 | 메뉴 |
1 | 한식 |
2 | 중식 |
3 | 일식 |
4 | 양식 |
코드 | 요일 | 시간 | 금액 |
1 | 1 | 1 | 30000 |
1 | 2 | 1 | 50000 |
2 | 1 | 1 | 30000 |
2 | 2 | 1 | 40000 |
시간 1 = 오전
시간 2 = 오후
요일 1 = 평일
요일 2 = 주말
두개의 테이블을 합쳐서 이런식으로 구현하려 하는데 어떤식으로 해야하는지 감이 잘안오네요..
메뉴 | 평일오전 | 주말오전 |
한식 | 30000 | 50000 |
중식 | 30000 | 40000 |
|
|
Comment 4
-
이리
2016.03.28 13:28
-
Blue2
2016.03.28 14:42
아직 초보라 검색해봐가면서 했는데 생각처럼 잘안되네용.. 조금만 더 자세히 알려주실수 있으신가요!
-
짱나부러
2016.03.28 14:54
직접 해보시는게 도움이 많이 되실텐데..............
;with test1 (code, menu) as
(
select 1, '한식' union
select 2, '중식' union
select 3, '일식' union
select 4, '양식'
),
test2 (code, d, h, amount) as
(
select 1, 1, 1, 30000 union
select 1, 2, 1, 50000 union
select 2, 1, 1, 30000 union
select 2, 2, 1, 40000
)
select a.menu
, sum(case when b.d = 1 and b.h = 1 then amount else 0 end) as [평일오전]
, sum(case when b.d = 1 and b.h = 2 then amount else 0 end) as [평일오후]
, sum(case when b.d = 2 and b.h = 1 then amount else 0 end) as [주말오전]
, sum(case when b.d = 2 and b.h = 2 then amount else 0 end) as [주말오후]
from test1 as a
join test2 as b on b.code = a.code
group by a.menu
-
한태
2016.03.29 20:56
create table menu(code int,menu varchar(10))insert into menu values(1, '한식'),(2,'중식'),(3,'일식'),(4,'양식')create table sellamount(code int,day int,time int,amount int)insert into sellamount values(1,1,1,30000),(2,1,1,30000),(2,2,1,40000),(1,2,1,50000)create clustered index menu_code on menu(code)create clustered index sellamount_code_day_time on sellamount(code,day,time)select a.menu, b.평일오전, b.평일오후, b.주말오전, b.주말오후from menu a inner join(select code, sum(case when day = 1 and time = 1 then amount else 0 end) as [평일오전], sum(case when day = 1 and time = 2 then amount else 0 end) as [평일오후], sum(case when day = 2 and time = 1 then amount else 0 end) as [주말오전], sum(case when day = 2 and time = 2 then amount else 0 end) as [주말오후]from sellamountgroup by code) bon a.code=b.code
join, sum, case문 등을 활용하시면 될거 같습니다.