SQL将一行拆分为四行

时间:2012-08-30 15:45:31

标签: sql

我有一个包含七列的SQL数据库,如下所示

version1; 1993; name; 233; 254; 291; 244
version1; 1994; name; 333; 354; 391; 344 

最后四列代表季度值,我想每行挑出一个值,因此每行都会被提取为四行。另外,我想添加一个值(跟踪所提供的季度的列)。希望上面的两个数据行报告为

version1; 1993; 1; name, 233
version1; 1993; 2; name; 254
version1; 1993; 3; name; 291
version1; 1993; 4; name; 244
version1; 1994; 1; name, 333
version1; 1994; 2; name; 354
version1; 1994; 3; name; 391
version1; 1994; 4; name; 344

有没有简单的方法可以做到这一点?

提前致谢

7 个答案:

答案 0 :(得分:1)

这将为您提供所需格式的输出:

SELECT version, year, 1 as 'quarter', name, quarter1_value 
FROM myTable

UNION

SELECT version, year, 2 as 'quarter', name, quarter2_value 
FROM myTable

UNION

SELECT version, year, 3 as 'quarter', name, quarter3_value 
FROM myTable

UNION

SELECT version, year, 4 as 'quarter', name, quarter4_value 
FROM myTable

答案 1 :(得分:1)

您可以使用UNION来获得所需的结果。这适用于MySQL和SQL Server。您可以像这样使用UNION

SELECT * FROM
(
SELECT version, year, 1 as 'quarter', name, quarter1_value FROM tt
UNION
SELECT version, year, 2 as 'quarter', name, quarter2_value FROM tt
UNION
SELECT version, year, 3 as 'quarter', name, quarter3_value FROM tt
UNION
SELECT version, year, 4 as 'quarter', name, quarter4_value FROM tt
) a
Order by Year, QUARTER

See this SQLFiddle

答案 2 :(得分:1)

您可以使用UNION ALLUNPIVOT。并非所有RDBMS都提供UNPIVOT

UNION ALL(见SQL Fiddle with Demo):

select col1, col2, col3, 1 quarter, col4
from yourtable
union all
select col1, col2, col3, 2 quarter, col5
from yourtable
union all
select col1, col2, col3, 3 quarter, col6
from yourtable
union all
select col1, col2, col3, 4 quarter, col7
from yourtable

UNPIVOT(见SQL Fiddle with Demo):

select col1, col2, col3, 
  cast(replace(field, 'col', '') as int) - 3 quarter, 
value
from 
(
  select col1, col2, col3, col4, col5, col6, col7

  from yourtable
) u
unpivot
(
  value 
  for field in (col4, col5, col6, col7)
)u

答案 3 :(得分:0)

SELECT version, year, name, '1', quarter_1 FROM table

UNION

SELECT version, year, name, '2', quarter_2 FROM table

UNION

...

答案 4 :(得分:0)

您可以使用四个季度进行交叉连接到表格,然后使用案例陈述来选择适当的季度:

SELECT
  ORIG_TABLE.VERSION,
  ORIG_TABLE.YEAR,
  QUARTER_NAMES.Q_NAME,
  ORIG_TABLE.NAME,
  CASE QUARTER_NAMES.Q_NAME
    WHEN 1 
      THEN ORIG_TABLE.Q1_VALUE
    WHEN 2
      THEN ORIG_TABLE.Q2_VALUE
    WHEN 3
      THEN ORIG_TABLE.Q3_VALUE
    WHEN 4
      THEN ORIG_TABLE.Q4_VALUE
 END Q_VALUE
FROM
  ORIG_TABLE
    CROSS JOIN 
    (
       SELECT 1 Q_NAME
       UNION SELECT 2 Q_NAME
       UNION SELECT 3 Q_NAME
       UNION SELECT 4 Q_NAME
    ) QUARTER_NAMES

答案 5 :(得分:0)

您可以创建一个sql视图。使用sql split函数,它根据索引返回一行。您可以从链接http://geekswithblogs.net/AngelEyes/archive/2007/04/12/111504.aspx

获得一些帮助

答案 6 :(得分:0)

没有提供DBMS,所以为了以防万一,我将抛出几个特定于SQL Server的版本。

-- SAMPLE DATA
DECLARE @T TABLE (Version VARCHAR(15), Year INT, Name VARCHAR(5), Col1 INT, Col2 INT, Col3 INT, Col4 INT)
INSERT @T VALUES
    ('version1', 1993, 'name', 233, 254, 291, 244),
    ('version1', 1994, 'name', 333, 354, 391, 344)

-- CROSS APPLY VALUES
SELECT  Version, Year, RowNum, Name, Value
FROM    @T
        CROSS APPLY 
        (   VALUES 
                (Col1, 1), 
                (Col2, 2), 
                (Col3, 3), 
                (Col4, 4)
        ) t (Value, RowNum)

-- UNPIVOT
SELECT  Version, Year, REPLACE(RowNum, 'Col', '') [RowNum], Name, Value
FROM    @T
        UNPIVOT
        (   Value
            FOR RowNum IN ([Col1], [Col2], [Col3], [Col4])
        ) upvt