This is the data:
id name period data1 data2
===================================================
111 name1 monthly aaaaa bbbbb
111 name1 quaterly ccccc ddddd
111 name1 halfYearly eeeee fffff
111 name1 annually ggggg hhhhh
I want query which fetch data in a single row like
id name monthlYdata1 monthlYdata2 quaterlydata1 quaterlydata2 halfYearlydata1 halfYearlydata2 annuallydata1 annuallydata2
==========================================================================================================================================================
111 name1 aaaaa bbbbb ccccc ddddd eeeee fffff ggggg hhhhh
You did not specify what RDBMS you are using but, this will work in all of them:
See SQL Fiddle with Demo
If you are using an RDBMS that has a
PIVOTfunction, then you can do the following which uses both anUNPIVOTandPIVOTto produce the results. As Andriy M pointed out theUNPIVOTis assuming that the datatype for bothdata1anddata2are the same types, if not, then a conversion would need to take place toUNPIVOTthe data:Oracle 11g:
See SQL Fiddle with Demo
SQL Server: