Search
 
SCRIPT & CODE EXAMPLE
 

SQL

convert rows to columns in sql server

-- convert rows to columns in sql server (PIVOT)
SELECT Firstname, Amount, PostalCode
FROM (
         SELECT value, columnname
         FROM yourtable
     ) d
PIVOT (
    max(value)
    FOR columnname IN (Firstname, Amount, PostalCode)
) piv;
Comment

Convert columns to rows in SQL Server

# You can use the UNPIVOT function to convert the columns into rows:

select id, entityId,
  indicatorname,
  indicatorvalue
from yourtable
unpivot
(
  indicatorvalue
  for indicatorname in (Indicator1, Indicator2, Indicator3)
) unpiv;

# Note, the datatypes of the columns you are unpivoting must be the same so you might 
# have to convert the datatypes prior to applying the unpivot.

# You could also use CROSS APPLY with UNION ALL to convert the columns:

select id, entityid,
  indicatorname,
  indicatorvalue
from yourtable
cross apply
(
  select 'Indicator1', Indicator1 union all
  select 'Indicator2', Indicator2 union all
  select 'Indicator3', Indicator3 union all
  select 'Indicator4', Indicator4 
) c (indicatorname, indicatorvalue);

# Depending on your version of SQL Server you could even use CROSS APPLY with 
# the VALUES clause:

select id, entityid,
  indicatorname,
  indicatorvalue
from yourtable
cross apply
(
  values
  ('Indicator1', Indicator1),
  ('Indicator2', Indicator2),
  ('Indicator3', Indicator3),
  ('Indicator4', Indicator4)
) c (indicatorname, indicatorvalue);

# Finally, if you have 150 columns to unpivot and 
# you don't want to hard-code the entire query, then you could generate the 
# sql statement using dynamic SQL:

DECLARE @colsUnpivot AS NVARCHAR(MAX),
   @query  AS NVARCHAR(MAX)

select @colsUnpivot 
  = stuff((select ','+quotename(C.column_name)
           from information_schema.columns as C
           where C.table_name = 'yourtable' and
                 C.column_name like 'Indicator%'
           for xml path('')), 1, 1, '')

set @query 
  = 'select id, entityId,
        indicatorname,
        indicatorvalue
     from yourtable
     unpivot
     (
        indicatorvalue
        for indicatorname in ('+ @colsunpivot +')
     ) u'

exec sp_executesql @query;
Comment

PREVIOUS NEXT
Code Example
Sql :: oracle add auto_increment column to existing table 
Sql :: datepart postgres 
Sql :: docker open terminal mysql server 
Sql :: mysql get all tables row count 
Sql :: trim leading zeros in sql 
Sql :: query saurce oracle 
Sql :: SELECT table_name FROM user_tables; 
Sql :: drop multiple databases mysql 
Sql :: where date in datetime mysql 
Sql :: insert data in pgsql 
Sql :: sql server substring 
Sql :: mysql change collation one column 
Sql :: mysql install with docker 
Sql :: temp table sql 
Sql :: postgres add column integer 
Sql :: mysql CAST(amount as float) 
Sql :: sql duplicate rows 
Sql :: how to print mysql query of codeigniter query builder 
Sql :: create or replace table sql 
Sql :: copy table postgres 
Sql :: mysql alter table add column first 
Sql :: min salary in sql 
Sql :: renombrar tabla mysql 
Sql :: How to drop a foreign key constraint in mysql ? 
Sql :: create stored procedure 
Sql :: n highest salary in sql 
Sql :: sql query to get the number of rows in a table 
Sql :: java sql timestamp now 
Sql :: mysql isnull 
Sql :: sql convert varchar to date 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =