SQL 질문과 답변 게시판
하루 꼬박 쿼리를 만들어 봤는데. 도저히 안되겠네요..ㅜ.ㅜ;
테이블 A에
idx gName bMoney gCnt WriteDay
란 컬럼이 있습니다.
물건을 살떄..
한꺼번에 1개 이상 즉.. 2,3 씩 그이상도 구매 가능한 몰을 만들었는데요..
일자별 수익통계를 내야 하는데..
idx gName bMoney gCnt WriteDay
1 상품1 10,0000 1 2010-09-12
2 상품2 10,0000 1 2010-09-12
3 상품3 10,0000 1 2010-09-12
4 상품1 20,0000 2 2010-09-12
5 상품1 20,0000 2 2010-09-12
6 상품1 20,0000 2 2010-09-12
7 상품1 50,0000 3 2010-09-13
8 상품1 50,0000 3 2010-09-13
9 상품1 50,0000 3 2010-09-13
10 상품1 30,0000 4 2010-09-13
11 상품1 30,0000 4 2010-09-13
이런식에 데이터가 있구요.. 통계는
gCnt 1를 하나로 보고.. 즉..
gCnt 값 1이 3개지만 하나로 보고 10,000
gCnt 값 2이 3개지만 하나로 보고 20,000
gCnt 값 3이 3개지만 하나로 보고 50,000
gCnt 값 4이 2개지만 하나로 보고 30,000
해서 통계를 내면
2010-09-13 80,0000
2010-09-12 30,0000
이렇게 뽑아야져 나와야 되구요..
다시설명하자면.. gCnt 값을 Distinct 해서 통계를 내야 하는데.. 잘 안되네요..
어떻게 쿼리를 만들어야 할까요...


gcnt가 주문 번호 역활을 하는듯 합니다.
with #test
as (
select 1 idx ,'상품' gName ,100000 bMoney ,1 gCnt ,'2010-09-12' WriteDay union all
select 2 ,'상품' ,100000 ,1 ,'2010-09-12' union all
select 3 ,'상품' ,100000 ,1 ,'2010-09-12' union all
select 4 ,'상품' ,200000 ,2 ,'2010-09-12' union all
select 5 ,'상품' ,200000 ,2 ,'2010-09-12' union all
select 6 ,'상품' ,200000 ,2 ,'2010-09-12' union all
select 7 ,'상품' ,500000 ,3 ,'2010-09-13' union all
select 8 ,'상품' ,500000 ,3 ,'2010-09-13' union all
select 9 ,'상품' ,500000 ,3 ,'2010-09-13' union all
select 10 ,'상품' ,300000 ,4 ,'2010-09-13' union all
select 11 ,'상품' ,300000 ,4 ,'2010-09-13' )
select r.WriteDay , SUM(r.bMoney) bMoney
from (
-- select distinct gCnt, bMoney , WriteDay
-- from #test
select gCnt, bMoney , WriteDay
from #test group by gCnt,bMoney , WriteDay
) r
group by r.WriteDay