|
[code=sql]USE tempdb
GO
IF OBJECT_ID('TEST') IS NOT NULL
DROP TABLE TEST;
GO
CREATE TABLE TEST
(
代码 CHAR(8),
日期 DATE,
指标名称 NVARCHAR(10),
指标数值 MONEY
);
GO
INSERT INTO TEST VALUES('1','1990-12-31','货币资金',7648000);
INSERT INTO TEST VALUES('1','1990-12-31','应收账款',56280000);
INSERT INTO TEST VALUES('1','1990-12-31','总资产',104912000);
INSERT INTO TEST VALUES('1','1991-12-31','货币资金',8648000);
INSERT INTO TEST VALUES('1','1991-12-31','应收账款',57280000);
INSERT INTO TEST VALUES('1','1991-12-31','总资产',105912000);
INSERT INTO TEST VALUES('1','1992-12-31','货币资金',9648000);
INSERT INTO TEST VALUES('1','1992-12-31','应收账款',58280000);
INSERT INTO TEST VALUES('1','1992-12-31','总资产',106912000);
INSERT INTO TEST VALUES('2','1990-12-31','货币资金',10648000);
INSERT INTO TEST VALUES('2','1990-12-31','应收账款',59280000);
INSERT INTO TEST VALUES('2','1990-12-31','总资产',107912000);
INSERT INTO TEST VALUES('2','1991-12-31','货币资金',11648000);
INSERT INTO TEST VALUES('2','1991-12-31','应收账款',60280000);
INSERT INTO TEST VALUES('2','1991-12-31','总资产',108912000);
INSERT INTO TEST VALUES('2','1992-12-31','货币资金',12648000);
INSERT INTO TEST VALUES('2','1992-12-31','应收账款',61280000);
INSERT INTO TEST VALUES('2','1992-12-31','总资产',109912000);
GO
----------------------------------------
;WITH A AS
(SELECT * FROM TEST PIVOT (MAX(指标数值) FOR 指标名称 IN(货币资金,应收账款,总资产))P),
B AS (SELECT 代码,日期,'货币资金/总资产' AS 指标名称,货币资金/总资产 AS 指标数值 FROM A),
C AS (SELECT 代码,日期,'应收账款/总资产' AS 指标名称,应收账款/总资产 AS 指标数值 FROM A)
SELECT * FROM B UNION ALL SELECT * FROM C ORDER BY 1,2
----------------------------------------
/*
代码 日期 指标名称 指标数值
-------- ---------- --------------- ---------------------
1 1990-12-31 货币资金/总资产 0.0728
1 1990-12-31 应收账款/总资产 0.5364
1 1991-12-31 货币资金/总资产 0.0816
1 1991-12-31 应收账款/总资产 0.5408
1 1992-12-31 应收账款/总资产 0.5451
1 1992-12-31 货币资金/总资产 0.0902
2 1990-12-31 应收账款/总资产 0.5493
2 1990-12-31 货币资金/总资产 0.0986
2 1991-12-31 货币资金/总资产 0.1069
2 1991-12-31 应收账款/总资产 0.5534
2 1992-12-31 应收账款/总资产 0.5575
2 1992-12-31 货币资金/总资产 0.115
(12 行受影响)
*/[/code] |
|