Create a row for each cell that has data from multiple columns

a 夏天 提交于 2019-12-08 05:55:33

问题


I'm dealing with data that has been generated from a survey that has a unique respondant ID as the first column and then has multiple columns relating to the choices of country that the respondent was looking at in relation to finding employees. So my table looks something like:

RespondentID  Andorra  Austria  Belgium  Cyprus  Denmark  Finland  France   

2546078180    Andorra  NULL     NULL     Cyprus  NULL     NULL     NULL 
2546077668    NULL     NULL     Belgium  NULL    NULL     NULL     NULL 
2546077120    NULL     NULL     NULL     NULL    Denmark  Finland  NULL 

What I want to end up with is a table that lists the Respondent ID for each answer given. So on the above data it would look like:

RespondentID  Country   

2546078180    Andorra
2546078180    Cyprus
2546077668    Belgium
2546077120    Denmark
2546077120    Finland   

As this should allow me to create a table detailing the countries that a respondent is linked with and then I can join this table to the other responses which were mostly yes/no or single answers which we need to do reporting on the data.

This data is coming in via an Excel spreadsheet so it would also be acceptable to do some formatting prior to the import into SQL if this is required or better.


回答1:


Use UNPIVOT to normalize your table:

SELECT u.RespondentID, u.Country
FROM @source
UNPIVOT (Country FOR c IN (Andorra, Austria, Belgium, Cyprus, Denmark, Finland, France)) u

@source is a table that contains the data imported from your Excel worksheet.

Test data:

DECLARE @source TABLE
(
    RespondentID BIGINT NOT NULL,
    Andorra VARCHAR(25),
    Austria VARCHAR(25),
    Belgium VARCHAR(25),
    Cyprus VARCHAR(25),
    Denmark VARCHAR(25),
    Finland VARCHAR(25),
    France VARCHAR(25)
)

INSERT INTO @source 
(RespondentID, Andorra,   Austria, Belgium,  Cyprus,   Denmark,    Finland,   France)
VALUES
(2546078180,   'Andorra', NULL,    NULL,     'Cyprus', NULL,       NULL,      NULL),
(2546077668,   NULL,      NULL,    'Belgium', NULL,    NULL,       NULL,      NULL),
(2546077120,   NULL,      NULL,    NULL,      NULL,    'Denkmark', 'Finland', NULL)

-- I assume that 'NULL' cell values from your Excel sheet become NULL during the import.

Output:

RespondentId         Country
-------------------- -------------------------
2546078180           Andorra
2546078180           Cyprus
2546077668           Belgium
2546077120           Denkmark
2546077120           Finland



回答2:


UNION clause is the way to go:

  SELECT * FROM (
    SELECT RespondentID, Field1 as Country
    FROM myTable
    UNION
    SELECT RespondentID, Field2 as Country
    FROM myTable
    UNION
    ....
    UNION
    SELECT RespondentID, Fieldn as Country
    FROM myTable) t
  WHERE Country IS NOT NULL


来源:https://stackoverflow.com/questions/25682396/create-a-row-for-each-cell-that-has-data-from-multiple-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!