구분별로 존재하는 값을 하나의 row로 출력하고자 할 때 MAX() 함수가 유용하게 사용된다.
예시 1
테이블 내용
SELECT * FROM lt_exchange_rate
le_no |
le_nm |
le_won |
1 |
USD |
1400 |
2 |
CNY |
169.23 |
3 |
JPY |
10 |
4 |
EUR |
1320 |
결과
SELECT
MAX(CASE WHEN le_nm='USD' THEN le_won END) AS 'v_usd',
MAX(CASE WHEN le_nm='CNY' THEN le_won END) AS 'v_cny',
MAX(CASE WHEN le_nm='JPY' THEN le_won END) AS 'v_jpy',
MAX(CASE WHEN le_nm='EUR' THEN le_won END) AS 'v_eur'
FROM lt_exchange_rate
v_usd |
v_cny |
v_jpy |
v_eur |
1400 |
169.23 |
10 |
1320 |
예시 2
테이블 내용
SELECT * FROM conf_mainpage
no_cm |
device |
goods_type |
url |
1 |
pc |
j |
list.php?jts=j&c=017004 |
2 |
pc |
t |
list.php?jts=t&c=018003 |
3 |
pc |
s |
list.php?jts=s&c=019006 |
4 |
mobile |
j |
list.php?jts=j&c=017004 |
5 |
mobile |
t |
list.php?jts=t&c=018003 |
6 |
mobile |
s |
list.php?jts=s&c=019006 |
결과
SELECT
MAX(CASE WHEN goods_type='j' THEN url END) AS 'j',
MAX(CASE WHEN goods_type='t' THEN url END) AS 't',
MAX(CASE WHEN goods_type='s' THEN url END) AS 's'
FROM conf_mainpage
WHERE device='pc'
j |
t |
s |
list.php?jts=j&c=017004 |
list.php?jts=t&c=018003 |
list.php?jts=s&c=019006 |