将列转换为具有sql server中各自数据的行

时间:2011-03-17 09:48:57

标签: sql-server rows

我有一个场景,我需要将表的列转换为行 例如 - 表 - 股票:

ScripName       ScripCode       Price   
-----------------------------------------
20 MICRONS      533022      39  

我需要用以下格式表示表格,但我只需要这种单行表示

ColName       ColValue
-----------------------------
ScripName      20 MICRONS
ScripCode      533022    
Price          39

这样我就可以直接将数据绑定到datalist控件。

7 个答案:

答案 0 :(得分:11)

听起来像UNPIVOT

网上书籍样本:

--Create the table and insert values as portrayed in the previous example.
CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,
    Emp3 int, Emp4 int, Emp5 int);
GO
INSERT INTO pvt VALUES (1,4,3,5,4,4);
INSERT INTO pvt VALUES (2,4,1,5,5,5);
INSERT INTO pvt VALUES (3,4,3,5,4,4);
INSERT INTO pvt VALUES (4,4,2,5,5,4);
INSERT INTO pvt VALUES (5,5,1,5,5,5);
GO
--Unpivot the table.
SELECT VendorID, Employee, Orders
FROM 
   (SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
   FROM pvt) p
UNPIVOT
   (Orders FOR Employee IN 
      (Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt;
GO

返回:

VendorID   Employee   Orders
---------- ---------- ------
1          Emp1       4
1          Emp2       3
1          Emp3       5
1          Emp4       4
1          Emp5       4
2          Emp1       4
2          Emp2       1
2          Emp3       5
2          Emp4       5
2          Emp5       5

另请参阅:Unpivot SQL thingieunpivot tag

答案 1 :(得分:7)

declare @T table (ScripName varchar(50), ScripCode varchar(50), Price int)
insert into @T values ('20 MICRONS', '533022', 39)

select 
  'ScripName' as ColName,
  ScripName as ColValue
from @T
union all
select 
  'ScripCode' as ColName,
  ScripCode as ColValue
from @T
union all
select 
  'Price' as ColName,
  cast(Price as varchar(50)) as ColValue
from @T

答案 2 :(得分:1)

select 'ScriptName', scriptName from table
union all
select 'ScriptCode', scriptCode from table
union all
select 'Price', price from table

答案 3 :(得分:1)

DECLARE @TABLE TABLE 
  (RowNo INT,ScripName  VARCHAR(10),ScripCode  VARCHAR(10)
  ,Price  VARCHAR(10))      
INSERT INTO @TABLE VALUES
  (1,'20 MICRONS ','533022','39')
SELECT ColumnName,ColumnValue from @Table
 Unpivot(ColumnValue For ColumnName IN (ScripName,ScripCode,Price)) AS H

答案 4 :(得分:0)

作为替代方案:

使用CROSS APPLY和VALUES只需一次传递表就可以非常简单有效地执行此操作(与为每列执行一次传递的联合查询不同)

SELECT
    ca.ColName, ca.ColValue
FROM YOurTable
CROSS APPLY (
      Values
         ('ScripName' , ScripName),
         ('ScripCode' , ScripCode),
         ('Price'     , cast(Price as varchar(50)) )
  ) as CA (ColName, ColValue)

我个人认为这种语法比使用unpivot更容易。

注意:您必须注意将所有源列转换为单值列的兼容类型

答案 5 :(得分:0)

我以这种方式解决了查询

SELECT
    ca.ID, ca.[Name]
FROM [Emp2]
CROSS APPLY (
      Values
         ('ID' , cast(ID as varchar)),
         ('[Name]' , Name)

  ) as CA (ID, Name)

输出看起来像

  ID     Name
------ --------------------------------------------------
ID     1
[Name] Joy
ID     2
[Name] jean
ID     4
[Name] paul

答案 6 :(得分:0)

CREATE TABLE #ORIGINAL
(
    COUNTRY VARCHAR(50),
    MALE_CRICKETER VARCHAR(50),
    FEMALE_CRICKETER VARCHAR(50),
    MALE_STAR VARCHAR(50),
    FEMALE_STAR VARCHAR(50),
)

select * from #ORIGINAL 
SELECT COUNTRY, ca.GENDER, ca.STAR, ca.CRICKETR
FROM #ORIGINAL
CROSS APPLY (
      Values
         ('M', MALE_CRICKETER, MALE_STAR),
         ('F', FEMALE_CRICKETER, FEMALE_STAR)

  ) as CA (GENDER, CRICKETR, STAR)