SQL Server 2017,如何将特定的行转换为列标题?

时间:2019-03-18 16:47:49

标签: sql sql-server-2017

导入平面.csv文件后,我确实清理了脏数据,然后留下了这个问题,其中每个“节”的第一列都必须是该组的ID。

这是该表在SQL中的外观:

a           |   b           |   c
-------------------------------------------
alpha 1011  |               |
1           |   10/20/2019  |   10/20/2020
2           |   10/20/2018  |   10/20/2019
3           |   10/20/2017  |   10/20/2018
4           |   10/20/2016  |   10/20/2017
alpha 1012  |               |
5           |   10/20/2015  |   10/20/2016
6           |   10/20/2014  |   10/20/2015
7           |   10/20/2013  |   10/20/2014
8           |   10/20/2012  |   10/20/2013
alpha 1013  |               |
9           |   10/20/2011  |   10/20/2012
10          |   10/20/2010  |   10/20/2011
11          |   10/20/2009  |   10/20/2010
12          |   10/20/2008  |   10/20/2009

以下是创建临时表的方法:

CREATE TABLE #Temp_CSV_Import
(
    a varchar(255),
    b varchar(255),
    c varchar(255)
)

INSERT INTO #Temp_CSV_Import (a, b, c) values ('alpha 1011', '', '')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('1', '10/20/2019', '10/20/2020')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('2', '10/20/2018', '10/20/2019')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('3', '10/20/2017', '10/20/2018')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('4', '10/20/2016', '10/20/2017')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('alpha 1012', '', '')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('5', '10/20/2015', '10/20/2016')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('6', '10/20/2014', '10/20/2015')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('7', '10/20/2013', '10/20/2014')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('8', '10/20/2012', '10/20/2013')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('alpha 1013', '', '')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('9', '10/20/2011', '10/20/2012')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('10', '10/20/2010', '10/20/2011')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('11', '10/20/2009', '10/20/2010')
INSERT INTO #Temp_CSV_Import (a, b, c) values ('12', '10/20/2008', '10/20/2009')

SELECT * FROM #Temp_CSV_Import

DROP TABLE #Temp_CSV_Import

然后我希望结果看起来像这样:

a  |    b          |    c          |    alpha
-----------------------------------------------
1  |    10/20/2019 |    10/20/2020 |    1011
2  |    10/20/2018 |    10/20/2019 |    1011
3  |    10/20/2017 |    10/20/2018 |    1011
4  |    10/20/2016 |    10/20/2017 |    1011
5  |    10/20/2015 |    10/20/2016 |    1012
6  |    10/20/2014 |    10/20/2015 |    1012
7  |    10/20/2013 |    10/20/2014 |    1012
8  |    10/20/2012 |    10/20/2013 |    1012
9  |    10/20/2011 |    10/20/2012 |    1013
10 |    10/20/2010 |    10/20/2011 |    1013
11 |    10/20/2009 |    10/20/2010 |    1013
12 |    10/20/2008 |    10/20/2009 |    1013

我可以使用SQL脚本获取结果表吗?

1 个答案:

答案 0 :(得分:0)

谢谢。我在临时表中添加了另一个IDENTITY列“ x”,然后想到了以下内容:

DECLARE @tempVal varchar(255), @intMin INT, @intMax INT, @tempAlphaVal varchar(15)

-- Assigning the first value in the dataset -- starting the loop
SET @intMin = (SELECT MIN(x) FROM #Temp_CSV_Import)

-- Assigning the last value in the dataset -- stoping the loop
SET @intMax = (SELECT Max(x) FROM #Temp_CSV_Import)

-- adding another column to the table
ALTER TABLE #Temp_CSV_Import ADD alpha varchar(15);

WHILE (@intMin <= @intMax)
BEGIN   
    -- find the value of column a in row @intMin
    SET @tempVal = (SELECT a FROM #Temp_CSV_Import WHERE x = @intMin)   

    -- check to see if the value has alpha  
    IF(@tempVal LIKE '%alpha%') 
        BEGIN           
            -- parse the string to get the number -- until the next alpha use this number
            SET @tempAlphaVal = (SELECT SUBSTRING(@tempVal, CHARINDEX(' ', @tempVal) +1, 20))           
        END
    ELSE
        BEGIN   
            -- update apha with the temp value in @tempAlphaVal     
            UPDATE #Temp_CSV_Import SET alpha = @tempAlphaVal WHERE x = @intMin
        END

    -- increment @intMin
    SET @intMin = @intMin + 1                   
END

-- lastly delete the alpha row since we do not need it anymore.
DELETE FROM #Temp_CSV_Import WHERE alpha is null

SELECT * FROM #Temp_CSV_Import