slq 쿼리를 통해 행과 열을 바꾸어 세로로 출력할수있을까요? 0 3 1,270

by 동숙옴 [SQL Query] sql [2019.07.16 16:37:39]


 

사진처럼 세로로출려된 db를 밑처럼 가로로 출력할수있을까요??

특정 db에만 있는 함수 말고 sql문을 통해 출력하고싶습니다... 디비는 db2사용하고있습니다

by kazetor [2019.07.16 16:50:22]

1. case 문

2. pivot

 


by ahriow [2019.07.16 17:02:09]
select 'Answer' type,
			max(case when date='2011' then answer end) as '2011',
          max(case when date='2012' then answer end) as '2012',
          max(case when date='2013' then answer end) as '2013',
          max(case when date='2014' then answer end) as '2014',
          max(case when date='2015' then answer end) as '2015',
          max(case when date='2016' then answer end) as '2016',
          max(case when date='2017' then answer end) as '2017'
from
(
select '2011' date , 'y' answer , 'y' qui union all
select '2012' date , 'y' answer , 'n' qui union all
select '2013' date , 'n' answer , 'n' qui union all
select '2014' date , 'y' answer , 'y' qui union all
select '2015' date , 'y' answer , 'y' qui union all
select '2016' date , 'n' answer , 'n' qui union all
select '2017' date , 'n' answer , 'y' qui
) a union all
select 'Qui' type,
		  max(case when date='2011' then qui end),
          max(case when date='2012' then qui end),
          max(case when date='2013' then qui end),
          max(case when date='2014' then qui end),
          max(case when date='2015' then qui end),
          max(case when date='2016' then qui end),
          max(case when date='2017' then qui end)
from
(
select '2011' date , 'y' answer , 'y' qui union all
select '2012' date , 'y' answer , 'n' qui union all
select '2013' date , 'n' answer , 'n' qui union all
select '2014' date , 'y' answer , 'y' qui union all
select '2015' date , 'y' answer , 'y' qui union all
select '2016' date , 'n' answer , 'n' qui union all
select '2017' date , 'n' answer , 'y' qui
) a ;




select b.type,
		  max(case when date='2011' and type = 'Answer' then answer when date='2011' and type = 'Qui' then qui end) as '2011',
		  max(case when date='2012' and type = 'Answer' then answer when date='2012' and type = 'Qui' then qui end) as '2012',
		  max(case when date='2013' and type = 'Answer' then answer when date='2013' and type = 'Qui' then qui end) as '2013',
		  max(case when date='2014' and type = 'Answer' then answer when date='2014' and type = 'Qui' then qui end) as '2014',
		  max(case when date='2015' and type = 'Answer' then answer when date='2015' and type = 'Qui' then qui end) as '2015',
		  max(case when date='2016' and type = 'Answer' then answer when date='2016' and type = 'Qui' then qui end) as '2016',
		  max(case when date='2017' and type = 'Answer' then answer when date='2017' and type = 'Qui' then qui end) as '2017'
from
(
select '2011' date , 'y' answer , 'y' qui union all
select '2012' date , 'y' answer , 'n' qui union all
select '2013' date , 'n' answer , 'n' qui union all
select '2014' date , 'y' answer , 'y' qui union all
select '2015' date , 'y' answer , 'y' qui union all
select '2016' date , 'n' answer , 'n' qui union all
select '2017' date , 'n' answer , 'y' qui
) a cross join (select 'Answer' type union all select 'Qui') b
group by b.type;

전 이렇게 무식하게밖에... ㅠㅠ


by 러드 [2019.07.16 17:05:34]

select max(case wher date = 2011 then val end) as yy_2011 ....이후 동일하게 년도만 변경

    from ( select 1 as gbn, date, amswer as val                       from   테이블

             union all

              select 2 as gbn, date,   qui as val 

                   from 테이블

              )

  group by gbn

 

휴대폰으로 적어서 오타는 대충 알아서 해주세요^^.

 

              

                

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입