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 :: postgres how to add field created at 
Sql :: ms sql server port 
Sql :: Write an SQL query to print details of the Workers whose SALARY lies between 100000 and 500000. 
Sql :: mysql Client does not support authentication protocol requested by server; consider upgrading MySQL client 
Sql :: sql select where 
Sql :: mysql query where in 
Sql :: SQL SELECT DISTINCT Statement 
Sql :: identify primary key in oracle table 
Sql :: mysql on duplicate key ignore 
Sql :: SQL BACKUP DATABASE for SQL Server 
Sql :: insert query in sql 
Sql :: insert or ignore postgres 
Sql :: restart mysql 
Sql :: postgres extract date from timestamp 
Sql :: update table sql multiple set 
Sql :: mysql not starting in xampp 
Sql :: how to import mysql database command line 
Sql :: postgresql delete all content 
Sql :: sql primary key constraint 
Sql :: how to get the maximum length of a name in sql 
Sql :: choose only one for each distinct collumn regardless of other columns 
Sql :: See Foreign Key 
Sql :: mysql show foreign keys column 
Sql :: sql insert into statement 
Sql :: Write SQL in ruby on rails 
Sql :: xampp mysql command to import a large database 
Sql :: select only distinct values from another table and excluding from current table 
Sql :: minus equivalent in my sql 
Sql :: sql server inner join convert collation 
Sql :: get number of rows in every table mysql 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =