矩阵转置 TSQL

发布于 2024-09-05 06:55:29 字数 232 浏览 4 评论 0原文

我们可以在标准 SQL2005/2008 中进行矩阵转置(行变成列,列变成行)吗?

1 2 3 4 5
4 5 6 6 7
7 8 9 8 9
1 3 4 5 6
2 4 5 6 7

更改

1 4 7 1 2
2 5 8 3 4 
3 6 9 5 6
4 6 8 5 6
5 7 9 6 7 

行数 <>列号 ?

让我们考虑一下它固定的行数。

Can we do matrix transpose (rows become columns and columns become rows) in standard SQL2005/2008?

1 2 3 4 5
4 5 6 6 7
7 8 9 8 9
1 3 4 5 6
2 4 5 6 7

changes to

1 4 7 1 2
2 5 8 3 4 
3 6 9 5 6
4 6 8 5 6
5 7 9 6 7 

how about no of rows <> no of column ?

let's consider the no of rows it's fixed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的那么颓废 2024-09-12 06:55:29

您可能想重新格式化您的问题,但如果您的数据采用以下形式,则转换很容易:

CREATE TABLE matrix (Row int NOT NULL, Column int NOT NULL, Value <datatype> NOT NULL)

SELECT Row AS Column
       ,Column AS Row
       ,Value
FROM matrix

You might want to reformat your question, but a transform is easy if your data is in the form:

CREATE TABLE matrix (Row int NOT NULL, Column int NOT NULL, Value <datatype> NOT NULL)

SELECT Row AS Column
       ,Column AS Row
       ,Value
FROM matrix
猫性小仙女 2024-09-12 06:55:29
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Result]') AND type in (N'U'))
DROP TABLE [dbo].[Result]
GO

CREATE TABLE [dbo].[Result](
    [RN] [int] NULL,
    [1] [int] NULL,
    [2] [int] NULL,
    [3] [int] NULL,
    [4] [int] NULL,
    [5] [int] NULL
) ON [PRIMARY]

GO

insert into Result
select 1,11,12,13,14,15
union all
select 2,21,22,23,24,25
union all
select 3,31,32,33,34,35
union all
select 4,41,42,43,44,45
union all
select 5,51,52,53,54,55

select * from Result


;WITH Preresult AS
(SELECT RN AS Row,
    Col,
    Val
FROM Result
UNPIVOT (Val FOR Col IN ([1],[2],[3],[4],[5])) unpvt
)
--select * from Preresult
--Transform array into matrix 
SELECT 
    Col as Row, 
    [1], 
    [2],
    [3],
    [4],
    [5]

FROM
    (
        SELECT Row, Col, Val FROM Preresult) t1
        PIVOT
        (MAX(Val) 
        FOR Row IN ([1],[2],[3],[4],[5])
        ) AS pvt
ORDER BY Row;-->replace Col for column sorting

来源:http://www.devx.com/dbzone/Article/ 40223/1763?supportItem=5

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Result]') AND type in (N'U'))
DROP TABLE [dbo].[Result]
GO

CREATE TABLE [dbo].[Result](
    [RN] [int] NULL,
    [1] [int] NULL,
    [2] [int] NULL,
    [3] [int] NULL,
    [4] [int] NULL,
    [5] [int] NULL
) ON [PRIMARY]

GO

insert into Result
select 1,11,12,13,14,15
union all
select 2,21,22,23,24,25
union all
select 3,31,32,33,34,35
union all
select 4,41,42,43,44,45
union all
select 5,51,52,53,54,55

select * from Result


;WITH Preresult AS
(SELECT RN AS Row,
    Col,
    Val
FROM Result
UNPIVOT (Val FOR Col IN ([1],[2],[3],[4],[5])) unpvt
)
--select * from Preresult
--Transform array into matrix 
SELECT 
    Col as Row, 
    [1], 
    [2],
    [3],
    [4],
    [5]

FROM
    (
        SELECT Row, Col, Val FROM Preresult) t1
        PIVOT
        (MAX(Val) 
        FOR Row IN ([1],[2],[3],[4],[5])
        ) AS pvt
ORDER BY Row;-->replace Col for column sorting

Source: http://www.devx.com/dbzone/Article/40223/1763?supportItem=5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文