group by 가 중첩됩니다. 0 3 3,689

by 망뎅이 [SQL Query] group by [2013.12.03 10:19:06]


select
 memberno, max(team1_cnt) team1_cnt, max(team2_cnt) team2_cnt, max(team1_firstday) team1_firstday, max(team1_lastday) team1_lastday, max(team2_firstday) team2_firstday, max(team2_lastday) team2_lastday
from (
 select
  memberno, count(*) team1_cnt, 0 team2_cnt, min(startday) team1_firstday, max(startday) team1_lastday, null team2_firstday, null team2_lastday
 from t_team1
 group by memberno
 union all
 select
  memberno, 0 team1_cnt, count(*) team2_cnt, null team1_firstday, null team1_lastday, min(startday) team2_firstday, max(startday) team2_lastday
 from t_team2
 group by memberno
)
group by memberno

위와 같이 쿼리를 작성하였는데, 쿼리의 결과는 맞게 나오지만, group by 절을 두번 중복하여 사용하여 깔끔하게 작성되었다는 생각이 들지 않습니다. 고수님들의 조언 부탁드립니다.
by 용근님 [2013.12.03 10:25:04]

쓸땐 써야죠


by 망뎅이 [2013.12.03 10:30:34]
그런가요 -_-; 쿼리가 느리진 않은데 별로 맘에 안드네요 -_ㅠ

by 마농 [2013.12.03 15:40:01]
가능은 합니다.
Group By > Union > Group By 하신걸
최초 그룹바이 빼시고 바로 Union > Group By 하실수도 있겠지요.
그러나 이 경우 전후 성능비교를 반드시 해보셔야 할 것입니다.
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입