0

我有一个包含七列的 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

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

提前致谢

4

7 回答 7

1

您可以为此使用 aUNION ALL或 an UNPIVOTUNPIVOT并非在所有 RDBMS 上都可用。

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
于 2012-08-30T16:00:42.970 回答
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
于 2012-08-30T15:50:30.657 回答
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

看到这个 SQLFiddle

于 2012-08-30T15:56:26.730 回答
0
SELECT version, year, name, '1', quarter_1 FROM table

UNION

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

UNION

...
于 2012-08-30T15:50:04.117 回答
0

您可以对包含四个季度的表进行交叉连接,然后使用 case 语句选择适当的季度:

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
于 2012-08-30T15:52:25.923 回答
0

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

于 2012-08-30T15:53:10.647 回答
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
于 2012-08-30T15:57:57.620 回答