今天有个同事找我,他说他有个需求,需要进行行转列,但是又跟一般的行转列有些区别,具体需求如下所说,需要将表1的数据转换为表2的显示格式.
我想了一下,给出了一个解决方法,具体如下所示(先给出测试数据)
INSERT INTO TEST
SELECT 1, 1, '定型名称', '预定型' UNIONALL
SELECT 1, 2, '进布方式', '调平' UNIONALL
SELECT 1, 3, '21米长定型机开机速度', '25' UNIONALL
SELECT 1, 4, '烘箱温度', '195' UNIONALL
SELECT 1, 5, '门幅(CM)', '200-210-210' UNIONALL
SELECT 2, 1, '过软', 'na' UNIONALL
SELECT 2, 2, '调平', 'na' UNIONALL
SELECT 2, 3, '25', '+/-0.5' UNIONALL
SELECT 2, 4, '150', '+/-5℃头尾烘箱除外' UNIONALL
SELECT 2, 5, '188-198-198', '+/-3'
实现其功能的SQL语句如下所示
WITH T
AS
(
SELECTNO, ROW_NUMBER() OVER (PARTITION BYNOORDERBY STEP) AS ROWID, NAME, VAlUEFROM TEST
)
SELECTNO, MAX(NAME) AS NAME, MAX(VALUE) ASVALUE, MAX(NAME2) AS NAME2 , MAX(VALUE2) AS VALUE2
新闻热点
疑难解答