如何从SQL查询中转置行和列

如何从SQL查询中转置行和列,sql,dynamic,transpose,Sql,Dynamic,Transpose,如何从SQL查询中转置行和列,如下所示。使用SQL Server 2012编写查询,即使将数据添加到数据库表中也能正常工作 试试这个: SELECT * FROM t_TableName GROUP BY Month 试一试 MS SQL Server 2008架构设置: CREATE TABLE yourtable ( [mon] varchar(3), [to] float, [aapl] float, [msft] float, [ko] floa

如何从SQL查询中转置行和列,如下所示。使用SQL Server 2012编写查询,即使将数据添加到数据库表中也能正常工作

试试这个:

SELECT * FROM t_TableName GROUP BY Month
试一试

MS SQL Server 2008架构设置

CREATE TABLE yourtable
    ( 
       [mon] varchar(3), [to] float, [aapl] float,
       [msft] float, [ko] float
    )
;

INSERT INTO yourtable
([mon],[to],[aapl],[msft],[ko])
VALUES
    ('jan', 12.25,112.25,56.52,45 ),
    ('feb', 12.52,125.25,56.87,46),
    ('mar', 12.65,126,56.94,47),
    ('apr', 12.95,127,56.99,45),
    ('may', 12.85,129,57.25,44)
;
select * from yourtable
| mon |    to |   aapl |  msft | ko |
|-----|-------|--------|-------|----|
| jan | 12.25 | 112.25 | 56.52 | 45 |
| feb | 12.52 | 125.25 | 56.87 | 46 |
| mar | 12.65 |    126 | 56.94 | 47 |
| apr | 12.95 |    127 | 56.99 | 45 |
| may | 12.85 |    129 | 57.25 | 44 |
select *
from
(
  select *
  from yourtable
  unpivot
  (
    value
    for month in ([to],[aapl],[msft],[ko])
  ) unpiv
) src
pivot
(
  max(value)
  for mon in (jan, feb, mar, apr, may)
) piv 
| month |    jan |    feb |   mar |   apr |   may |
|-------|--------|--------|-------|-------|-------|
|  aapl | 112.25 | 125.25 |   126 |   127 |   129 |
|    ko |     45 |     46 |    47 |    45 |    44 |
|  msft |  56.52 |  56.87 | 56.94 | 56.99 | 57.25 |
|    to |  12.25 |  12.52 | 12.65 | 12.95 | 12.85 |
查询1

CREATE TABLE yourtable
    ( 
       [mon] varchar(3), [to] float, [aapl] float,
       [msft] float, [ko] float
    )
;

INSERT INTO yourtable
([mon],[to],[aapl],[msft],[ko])
VALUES
    ('jan', 12.25,112.25,56.52,45 ),
    ('feb', 12.52,125.25,56.87,46),
    ('mar', 12.65,126,56.94,47),
    ('apr', 12.95,127,56.99,45),
    ('may', 12.85,129,57.25,44)
;
select * from yourtable
| mon |    to |   aapl |  msft | ko |
|-----|-------|--------|-------|----|
| jan | 12.25 | 112.25 | 56.52 | 45 |
| feb | 12.52 | 125.25 | 56.87 | 46 |
| mar | 12.65 |    126 | 56.94 | 47 |
| apr | 12.95 |    127 | 56.99 | 45 |
| may | 12.85 |    129 | 57.25 | 44 |
select *
from
(
  select *
  from yourtable
  unpivot
  (
    value
    for month in ([to],[aapl],[msft],[ko])
  ) unpiv
) src
pivot
(
  max(value)
  for mon in (jan, feb, mar, apr, may)
) piv 
| month |    jan |    feb |   mar |   apr |   may |
|-------|--------|--------|-------|-------|-------|
|  aapl | 112.25 | 125.25 |   126 |   127 |   129 |
|    ko |     45 |     46 |    47 |    45 |    44 |
|  msft |  56.52 |  56.87 | 56.94 | 56.99 | 57.25 |
|    to |  12.25 |  12.52 | 12.65 | 12.95 | 12.85 |

CREATE TABLE yourtable
    ( 
       [mon] varchar(3), [to] float, [aapl] float,
       [msft] float, [ko] float
    )
;

INSERT INTO yourtable
([mon],[to],[aapl],[msft],[ko])
VALUES
    ('jan', 12.25,112.25,56.52,45 ),
    ('feb', 12.52,125.25,56.87,46),
    ('mar', 12.65,126,56.94,47),
    ('apr', 12.95,127,56.99,45),
    ('may', 12.85,129,57.25,44)
;
select * from yourtable
| mon |    to |   aapl |  msft | ko |
|-----|-------|--------|-------|----|
| jan | 12.25 | 112.25 | 56.52 | 45 |
| feb | 12.52 | 125.25 | 56.87 | 46 |
| mar | 12.65 |    126 | 56.94 | 47 |
| apr | 12.95 |    127 | 56.99 | 45 |
| may | 12.85 |    129 | 57.25 | 44 |
select *
from
(
  select *
  from yourtable
  unpivot
  (
    value
    for month in ([to],[aapl],[msft],[ko])
  ) unpiv
) src
pivot
(
  max(value)
  for mon in (jan, feb, mar, apr, may)
) piv 
| month |    jan |    feb |   mar |   apr |   may |
|-------|--------|--------|-------|-------|-------|
|  aapl | 112.25 | 125.25 |   126 |   127 |   129 |
|    ko |     45 |     46 |    47 |    45 |    44 |
|  msft |  56.52 |  56.87 | 56.94 | 56.99 | 57.25 |
|    to |  12.25 |  12.52 | 12.65 | 12.95 | 12.85 |
查询2

CREATE TABLE yourtable
    ( 
       [mon] varchar(3), [to] float, [aapl] float,
       [msft] float, [ko] float
    )
;

INSERT INTO yourtable
([mon],[to],[aapl],[msft],[ko])
VALUES
    ('jan', 12.25,112.25,56.52,45 ),
    ('feb', 12.52,125.25,56.87,46),
    ('mar', 12.65,126,56.94,47),
    ('apr', 12.95,127,56.99,45),
    ('may', 12.85,129,57.25,44)
;
select * from yourtable
| mon |    to |   aapl |  msft | ko |
|-----|-------|--------|-------|----|
| jan | 12.25 | 112.25 | 56.52 | 45 |
| feb | 12.52 | 125.25 | 56.87 | 46 |
| mar | 12.65 |    126 | 56.94 | 47 |
| apr | 12.95 |    127 | 56.99 | 45 |
| may | 12.85 |    129 | 57.25 | 44 |
select *
from
(
  select *
  from yourtable
  unpivot
  (
    value
    for month in ([to],[aapl],[msft],[ko])
  ) unpiv
) src
pivot
(
  max(value)
  for mon in (jan, feb, mar, apr, may)
) piv 
| month |    jan |    feb |   mar |   apr |   may |
|-------|--------|--------|-------|-------|-------|
|  aapl | 112.25 | 125.25 |   126 |   127 |   129 |
|    ko |     45 |     46 |    47 |    45 |    44 |
|  msft |  56.52 |  56.87 | 56.94 | 56.99 | 57.25 |
|    to |  12.25 |  12.52 | 12.65 | 12.95 | 12.85 |

CREATE TABLE yourtable
    ( 
       [mon] varchar(3), [to] float, [aapl] float,
       [msft] float, [ko] float
    )
;

INSERT INTO yourtable
([mon],[to],[aapl],[msft],[ko])
VALUES
    ('jan', 12.25,112.25,56.52,45 ),
    ('feb', 12.52,125.25,56.87,46),
    ('mar', 12.65,126,56.94,47),
    ('apr', 12.95,127,56.99,45),
    ('may', 12.85,129,57.25,44)
;
select * from yourtable
| mon |    to |   aapl |  msft | ko |
|-----|-------|--------|-------|----|
| jan | 12.25 | 112.25 | 56.52 | 45 |
| feb | 12.52 | 125.25 | 56.87 | 46 |
| mar | 12.65 |    126 | 56.94 | 47 |
| apr | 12.95 |    127 | 56.99 | 45 |
| may | 12.85 |    129 | 57.25 | 44 |
select *
from
(
  select *
  from yourtable
  unpivot
  (
    value
    for month in ([to],[aapl],[msft],[ko])
  ) unpiv
) src
pivot
(
  max(value)
  for mon in (jan, feb, mar, apr, may)
) piv 
| month |    jan |    feb |   mar |   apr |   may |
|-------|--------|--------|-------|-------|-------|
|  aapl | 112.25 | 125.25 |   126 |   127 |   129 |
|    ko |     45 |     46 |    47 |    45 |    44 |
|  msft |  56.52 |  56.87 | 56.94 | 56.99 | 57.25 |
|    to |  12.25 |  12.52 | 12.65 | 12.95 | 12.85 |

向我们展示您的愿望输出是什么,您尝试了什么,您的rdbms是什么。请阅读,这里是学习如何提高问题质量和获得更好答案的好地方。