microsoft sql server 에는 greatest 함수가 없다.
그래서 일반적으로 dummy 테이블과 cross join해서 max를 치는 기법(=기법1)을 사용한다.
그런데, 이 기법은 좀 귀찮다. 귀찮음을 달래기 위해 다음의 기법(=기법2)을 소개한다.
데이터
create table #temp(
col1 int
, col2 int
, col3 int
, col4 int
, col5 int
)
insert #temp values(1,2,3,4,5)
기법1
select
max(
case
when b.col = 1 then col1
when b.col = 2 then col2
when b.col = 3 then col3
when b.col = 4 then col4
when b.col = 5 then col5
end
) max_value
from #temp a
cross join (values (1),(2),(3),(4),(5)) b (col)
기법2
select
max(b.col) max_value
from #temp a
cross apply (values (a.col1),(a.col2),(a.col3),(a.col4),(a.col5)) b (col)
기법1보다 기법2가 좋은 점은?
- 코딩량이 약간 줄어든다.
- 성능은 같다고 봐도 무방하다.
- 가독성은? 잘 모르겠다.