将不同的列值放入一个列中

时间:2018-02-20 03:21:15

标签: sql sql-server ssms

enter image description here我有5列,我希望从这5列中获取值并将它们放入一列中。

+---------------+----------+----------+----------+---------------+
| Option_1      | Option_2 | Option_3 | Option_4 | Option_5      |
+---------------+----------+----------+----------+---------------+
| Below average | Average  | Good     | Better   | Above average |
+---------------+----------+----------+----------+---------------+

所以在输出中我希望在一列中低于平均值,平均值,良好,更好和高于平均值命名选项

4 个答案:

答案 0 :(得分:0)

全部联盟可以完成:

SELECT
  Option1 AS Options
FROM (SELECT
  option1
FROM tablename
UNION ALL
SELECT
  option2
FROM tablename
UNION ALL
SELECT
  option3
FROM tablename
UNION ALL
SELECT
  option4
FROM tablename
UNION ALL
SELECT
  option5
FROM tablename) tablename;

UNPIVOT通过将列旋转到行中来执行PIVOT的几乎相反的操作。

SELECT
  opt.Options
FROM (SELECT
  *
FROM yourtable) t
UNPIVOT (Options FOR tables IN ([Option_1], [Option_2], [Option_3], [Option_4], [Option_5])) AS opt
ORDER BY tables;

output

有关详细信息,请访问:https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot

答案 1 :(得分:0)

SELECT CONCAT(Option_1,', ',Option_2,', ',Option_3,' ,',Option_4,',',Option_5) as Option
FROM TAble1

如果您希望结果为一列但多行

SELECT Option_1 AS Option     FROM Table1
UNION ALL
SELECT Option_2  AS Option  FROM  Table1
UNION ALL  
SELECT Option_3 AS Option  FROM Table1
UNION ALL  
SELECT Option_4 AS Option  FROM Table1
UNION ALL  
SELECT Option_5  AS Option  FROM Table1

使用UNPIVOT

SELECT
  rslt.Options
FROM (SELECT
  *
FROM Table1 ) AS dt
UNPIVOT (Options FOR tables IN ([Option_1], [Option_2], [Option_3], [Option_4], [Option_5])) AS rslt

示例Link

答案 2 :(得分:0)

declare @table table( Option_1 varchar(50),Option_2 varchar(50), Option_3 varchar(50), Option_4 varchar(50), Option_5  varchar(50))

insert into @table
select  'Below average', 'Average', 'Good','Better','Above average'

select p.typename,p.numericvalue from (
select * 
from @table
) a
unpivot(numericvalue for typename in([Option_1] ,[Option_2] , [Option_3] , [Option_4] , [Option_5]  )) as p
order by typename

输出

typename    numericvalue
Option_1    Below average
Option_2    Average
Option_3    Good
Option_4    Better
Option_5    Above average

答案 3 :(得分:0)

样本数据

IF OBJECT_ID('dbo.Sampletable')IS NOT NULL
DROP TABLE Sampletable
GO
CREATE TABLE Sampletable (
      Option_1 varchar(50)
     ,Option_2 varchar(50)
     ,Option_3 varchar(50)
     ,Option_4 varchar(50)
     ,Option_5  varchar(50)
     )

INSERT INTO Sampletable
SELECT  'Below average', 'Average', 'Good','Better','Above average'

使用Dynamic Sql和Cross apply我们得到预期的结果

DECLARE @DynamicCol nvarchar(max),@Sql nvarchar(max)

SELECT @DynamicCol=
STUFF((SELECT ', '+'('+''''+COLUMN_NAME+''''+','+ COLUMN_NAME +')'FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='Sampletable'
FOR XML PATH ('')),1,1,'')

SET @Sql='
SELECT   Typename
        ,Numericvalue 
FROM Sampletable t
CROSS APPLY (VALUES '+@DynamicCol+
    ') AS dt (Typename,Numericvalue )'
PRINT @Sql
EXEC (@Sql)

Sample demo 结果

Typename    Numericvalue
-------------------------
Option_1    Below average
Option_2    Average
Option_3    Good
Option_4    Better
Option_5    Above average