简体   繁体   English

将数据列转换为SQL Server中的数据行

[英]Convert Columns of Data to Rows of data in SQL Server

I'm trying to learn how to import data from a csv file to my database and then reorganise it into my tables. 我正在尝试学习如何将csv文件中的数据导入到我的数据库,然后将其重新组织到表中。 I have imported a very simple set of data into a table called ' CSVTest_Match' that looks like this: 我已经将一组非常简单的数据导入到名为“ CSVTest_Match”的表中,如下所示:

HEADER          DATA             --(Column Names)
--------------- --------------
Home Team       Barnsley
Away Team       Wigan
Kick Off Time   14:02
Kick Off Date   03/08/2013
Home Goals      0
Away Goals      4

The values in both columns are VARCHAR's at this point. 此时,两列中的值都是VARCHAR。 I would like to transform this data to look like this: 我想将此数据转换为如下形式:

HOMETEAM   AWAYTEAM   KICKOFFTIME   KICKOFFDATE   HOMEGOALS   AWAYGOALS  -- (Column Names)
---------- ---------- ------------- ------------- ----------- ----------
Barnsley   Wigan      14:02         03/08/2013    0           4

At this point it would be useful if the data was converted to VARCHAR, DATETIME, TINYINT values as appropriate. 此时,如果将数据适当地转换为VARCHAR,DATETIME,TINYINT值,将很有用。 I've been getting rather confused trying to work out how to use PIVOT to do this so I would really appreciate some help. 我一直很困惑,试图找出如何使用PIVOT来做到这一点,所以我非常感谢您的帮助。

EDIT: I finally figured it out. 编辑:我终于想通了。 The code required was: 所需的代码是:

SELECT * FROM 
(SELECT Header, Data FROM CSVTest_Match) AS T
PIVOT (Min(Data) FOR Header IN ([Home Team], [Away Team], [Kick Off Time], 
                [Kick Off Date], [Home Goals], [Away Goals])) AS T2

I don't remember where I swiped this from, probably from around here, but here you go, I've been using this for a while.. This assumes the column 'names' are Indicator1-x and the table is yourtable. 我不记得我是从哪里刷过的,可能是从这里刷过来的,但是在这里,您已经使用了一段时间了。.假设“名称”列为Indicator1-x,并且表为yourtable。 Search and replace accordingly. 搜索并进行相应替换。 If you don't know your header names ahead of time, do a select distinct on them, and then do C.column_name in 如果您不提前知道标题名称,请对它们进行选择,然后在其中输入C.column_name

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 = 'CSVTest_Match' and
                 C.column_name in ('Home Team','Away Team','Kick Off Time','Kick Off Date','Home Goals','Away Goals')
           for xml path('')), 1, 1, '')

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

exec sp_executesql @query;

" Convert Columns of Data to Rows of data in SQL Server ?". “在SQL Server中将数据列转换为数据行吗?”。 I started searching Google on your questions, I got these much answers. 我开始在Google搜索您的问题时得到了很多答案。

Visit these links: " Converting Columns into rows with their respective data in sql server " or " Convert row data to column in SQL Server " or " SQL query to convert columns into rows " or " How to convert Columns into Rows in Oracle? " or 请访问以下链接:“ 将列数据转换为带有sql server中相应数据的行 ”或“ 将行数据转换为SQL Server中的列 ”或“ 将列转换为行的SQL查询 ”或“ 如何在Oracle中将列转换为行? ”要么

http://forums.asp.net/t/1851916.aspx?Converting+Column+values+to+Rows+in+SQL+Query http://forums.asp.net/t/1851916.aspx?Converting+Column+values+to+Rows+in+SQL+Query

SELECT * FROM 
(SELECT Header, Data FROM CSVTest_Match) AS T
PIVOT (Min(Data) FOR Header IN ([Home Team], [Away Team], [Kick Off Time], 
                [Kick Off Date], [Home Goals], [Away Goals])) AS T2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM