oracle 格式转换 <行转列列转行>

一.效果图

 转换后

二.sql语法

  1.先是将你要的数据显示出来(当成一张表)

  如 

  SELECT time_test time1,
         STRUCTCODE,
         SUBSTRUCTCODE,
         COUNT (*) 片数,
         ROUND (AVG (COW_LOP1_AVG_K), 2) AS COW_LOP1_AVG_K,
         ROUND (AVG (COW_LOP1_AVG), 2) AS SMP_LOP1,
         ROUND (AVG (COW_LOP2_AVG), 2) AS SMP_LOP2,
         ROUND (AVG (COW_VF1_AVG), 2) SMP_VF1,
         ROUND (AVG (COW_WLD1_AVG), 2) SMP_WLD1,
         ROUND (AVG (COW_ESD04_YIELD), 2) MM400,
         ROUND (AVG (COW_VF4_AVG), 2) SMP_VF4,
         ROUND (AVG (COW_HW1_AVG), 2) SMP_HW1,
         ROUND (AVG (COW_IR1_YIELD), 2) SMP_IR1_YIELD,
         ROUND (AVG (COW_VZ1_AVG), 2) SMP_VZ1,
         ROUND (AVG (COW_ESD01_YIELD), 2) ESD01_YIELD,
         ROUND (AVG (COW_ESD03_YIELD), 2) ESD03_YIELD,
         ROUND (AVG (droop), 2) AS Droop
    FROM (SELECT DISTINCT SUBSTR (dm.COW_STARTTIME, 1, 10) time_test,
                          SUBSTR (EPI_OUTTIME, 1, 10) time_build,
                          SUBSTR (dm.lot, 3, 6) recipe,
                          b.STRUCTCODE,
                          b.SUBSTRUCTCODE,
                          dm.COW_LOP1_AVG,
                          dm.COW_LOP2_AVG,
                          dm.COW_LOP1_AVG / dm.COW_LOP2_AVG AS droop,
                          dm.COW_LOP1_AVG_K,
                          dm.COW_VF1_AVG,
                          dm.COW_WLD1_AVG,
                          dm.COW_WLD2_AVG,
                          dm.COW_VF4_AVG,
                          dm.COW_HW1_AVG,
                          dm.COW_IR1_YIELD,
                          dm.COW_VZ1_AVG,
                          dm.COW_ESD01_YIELD,
                          dm.COW_ESD03_YIELD,
                          dm.COW_ESD04_YIELD
            FROM CHIPDM.DM_SMP_PROBER dm,
                 (SELECT lot,
                         wo,
                         COMPONENTID,
                         STRUCTCODE,
                         SUBSTRUCTCODE
                    FROM epi.MES_EPI_WO_BOOK, epi.MES_WIP_COMP_CREATE
                   WHERE epi.MES_EPI_WO_BOOK.lot =
                            epi.MES_WIP_COMP_CREATE.CREATELOT) b,
                 mes_wms_lot_nonactive wms
           WHERE     dm.epicomponentid = wms.waferid
                 AND dm.COW_STARTTIME IS NOT NULL
                 AND dm.epicomponentid = b.COMPONENTID
                 AND dm.COW_LOP2_AVG <> 0
                 AND dm.COW_LOP2_AVG IS NOT NULL
                 AND SUBSTR (dm.COW_STARTTIME, 1, 10) >= ‘2019/12/09‘
                 AND SUBSTR (dm.COW_STARTTIME, 1, 10) <= ‘2020/01/02‘
                 AND SUBSTR (dm.lot, 3, 6) IN (‘W1028K‘)
                 AND SUBSTR (b.COMPONENTID, 12, 1) IN (‘L‘, ‘P‘, ‘Y‘)
                 AND SUBSTR (dm.LOT, 2, 1) IN (‘H‘,
                                               ‘I‘,
                                               ‘K‘,
                                               ‘L‘)
                 AND b.STRUCTCODE IN (‘W4L2D06‘, ‘W4L2D05‘)
                 AND b.SUBSTRUCTCODE IN (‘01‘,
                                         ‘02‘,
                                         ‘03‘,
                                         ‘04‘))
GROUP BY STRUCTCODE, SUBSTRUCTCODE, time_test
ORDER BY time_test

2.套语法(将你的表放入  你的sql表

 SELECT *
    FROM (SELECT time1,--根据时间排序
                 structcode || ‘-‘ || substructcode ss,--不做转换的
                 CASE
                    WHEN category = ‘片数‘ THEN ‘片数‘
                    WHEN category = ‘COW_LOP1_AVG_K‘ THEN ‘COW_LOP1_AVG_K‘
                    WHEN category = ‘SMP_LOP1‘ THEN ‘SMP_LOP1‘
                    WHEN category = ‘SMP_LOP2‘ THEN ‘SMP_LOP2‘
                    WHEN category = ‘SMP_VF1‘ THEN ‘SMP_VF1‘
                    WHEN category = ‘SMP_WLD1‘ THEN ‘SMP_WLD1‘
                    WHEN category = ‘MM400‘ THEN ‘MM400‘
                    WHEN category = ‘SMP_VF4‘ THEN ‘SMP_VF4‘
                    WHEN category = ‘SMP_HW1‘ THEN ‘SMP_HW1‘
                    WHEN category = ‘SMP_IR1_YIELD‘ THEN ‘SMP_IR1_YIELD‘
                    WHEN category = ‘SMP_VZ1‘ THEN ‘SMP_VZ1‘
                    WHEN category = ‘ESD01_YIELD‘ THEN ‘ESD01_YIELD‘--
                    WHEN category = ‘DROOP‘ THEN ‘DROOP‘--你想转哪个字段就填哪个字段 填了的下面 UNPIVOT (VALUE FOR category IN  (这里面就得填)
                 END
                    category,--你的SQL表里 要有字段 上面才能填
                 VALUE AS aa
            FROM (  

                       --你的SQL表

                  ) UNPIVOT (VALUE
                                      FOR category
                                      IN  (片数,
                                          COW_LOP1_AVG_K,
                                          SMP_LOP1,
                                          SMP_LOP2,
                                          SMP_VF1,
                                          SMP_WLD1,
                                          MM400,
                                          SMP_VF4,
                                          SMP_HW1,
                                          SMP_IR1_YIELD,
                                          SMP_VZ1,
                                          ESD01_YIELD,--和上面的字段名要相同

                                          Droop))) PIVOT (MAX (aa)
                                                   FOR time1
                                                   IN  (‘2019/12/09‘ AS "2019/12/09",
                                                       ‘2019/12/10‘ AS "2019/12/10",
                                                       ‘2019/12/11‘ AS "2019/12/11",
                                                       ‘2019/12/12‘ AS "2019/12/12",
                                                       ‘2019/12/13‘ AS "2019/12/13",
                                                       ‘2019/12/14‘ AS "2019/12/14",
                                                       ‘2019/12/15‘ AS "2019/12/15",
                                                       ‘2019/12/16‘ AS "2019/12/16",
                                                       ‘2019/12/17‘ AS "2019/12/17",
                                                       ‘2019/12/18‘ AS "2019/12/18",
                                                       ‘2019/12/19‘ AS "2019/12/19",
                                                       ‘2019/12/20‘ AS "2019/12/20",
                                                       ‘2019/12/21‘ AS "2019/12/21",
                                                       ‘2019/12/22‘ AS "2019/12/22",
                                                       ‘2019/12/23‘ AS "2019/12/23",
                                                       ‘2019/12/24‘ AS "2019/12/24",
                                                       ‘2019/12/25‘ AS "2019/12/25",
                                                       ‘2019/12/26‘ AS "2019/12/26",
                                                       ‘2019/12/27‘ AS "2019/12/27",
                                                       ‘2019/12/28‘ AS "2019/12/28",
                                                       ‘2019/12/29‘ AS "2019/12/29",
                                                       ‘2019/12/30‘ AS "2019/12/30",
                                                       ‘2019/12/31‘ AS "2019/12/31",
                                                       ‘2020/01/01‘ AS "2020/01/01",
                                                       ‘2020/01/02‘ AS "2020/01/02"))--分组列  这个我是后台sql 拼接的 动态的
ORDER BY ss,--排序不要在意
         DECODE (category,
                 ‘片数‘, 1,
                 ‘COW_LOP1_AVG_K‘, 2,
                 ‘SMP_LOP1‘, 3,
                 ‘SMP_LOP2‘, 4,
                 ‘SMP_VF1‘, 5,
                 ‘SMP_WLD1‘, 6,
                 ‘MM400‘, 7,
                 ‘SMP_VF4‘, 8,
                 ‘SMP_HW1‘, 9,
                 ‘SMP_IR1_YIELD‘, 10,
                 ‘SMP_VZ1‘, 11,
                 ‘ESD01_YIELD‘, 12,
                 ‘Droop‘, 13) ASC--自定义排序

3.合成的sql

/* Formatted on 2020/1/17 10:30:59  */
  SELECT *
    FROM (SELECT time1,
                 structcode || ‘-‘ || substructcode ss,
                 CASE
                    WHEN category = ‘片数‘ THEN ‘片数‘
                    WHEN category = ‘COW_LOP1_AVG_K‘ THEN ‘COW_LOP1_AVG_K‘
                    WHEN category = ‘SMP_LOP1‘ THEN ‘SMP_LOP1‘
                    WHEN category = ‘SMP_LOP2‘ THEN ‘SMP_LOP2‘
                    WHEN category = ‘SMP_VF1‘ THEN ‘SMP_VF1‘
                    WHEN category = ‘SMP_WLD1‘ THEN ‘SMP_WLD1‘
                    WHEN category = ‘MM400‘ THEN ‘MM400‘
                    WHEN category = ‘SMP_VF4‘ THEN ‘SMP_VF4‘
                    WHEN category = ‘SMP_HW1‘ THEN ‘SMP_HW1‘
                    WHEN category = ‘SMP_IR1_YIELD‘ THEN ‘SMP_IR1_YIELD‘
                    WHEN category = ‘SMP_VZ1‘ THEN ‘SMP_VZ1‘
                    WHEN category = ‘ESD01_YIELD‘ THEN ‘ESD01_YIELD‘
                    WHEN category = ‘DROOP‘ THEN ‘DROOP‘
                 END
                    category,
                 VALUE AS aa
            FROM (  SELECT time_test time1,
                           STRUCTCODE,
                           SUBSTRUCTCODE,
                           COUNT (*) 片数,
                           ROUND (AVG (COW_LOP1_AVG_K), 2) AS COW_LOP1_AVG_K,
                           ROUND (AVG (COW_LOP1_AVG), 2) AS SMP_LOP1,
                           ROUND (AVG (COW_LOP2_AVG), 2) AS SMP_LOP2,
                           ROUND (AVG (COW_VF1_AVG), 2) SMP_VF1,
                           ROUND (AVG (COW_WLD1_AVG), 2) SMP_WLD1,
                           ROUND (AVG (COW_ESD04_YIELD), 2) MM400,
                           ROUND (AVG (COW_VF4_AVG), 2) SMP_VF4,
                           ROUND (AVG (COW_HW1_AVG), 2) SMP_HW1,
                           ROUND (AVG (COW_IR1_YIELD), 2) SMP_IR1_YIELD,
                           ROUND (AVG (COW_VZ1_AVG), 2) SMP_VZ1,
                           ROUND (AVG (COW_ESD01_YIELD), 2) ESD01_YIELD,
                           ROUND (AVG (COW_ESD03_YIELD), 2) ESD03_YIELD,
                           ROUND (AVG (droop), 2) AS Droop
                      FROM (SELECT DISTINCT
                                   SUBSTR (dm.COW_STARTTIME, 1, 10) time_test,
                                   SUBSTR (EPI_OUTTIME, 1, 10) time_build,
                                   SUBSTR (dm.lot, 3, 6) recipe,
                                   b.STRUCTCODE,
                                   b.SUBSTRUCTCODE,
                                   dm.COW_LOP1_AVG,
                                   dm.COW_LOP2_AVG,
                                   dm.COW_LOP1_AVG / dm.COW_LOP2_AVG AS droop,
                                   dm.COW_LOP1_AVG_K,
                                   dm.COW_VF1_AVG,
                                   dm.COW_WLD1_AVG,
                                   dm.COW_WLD2_AVG,
                                   dm.COW_VF4_AVG,
                                   dm.COW_HW1_AVG,
                                   dm.COW_IR1_YIELD,
                                   dm.COW_VZ1_AVG,
                                   dm.COW_ESD01_YIELD,
                                   dm.COW_ESD03_YIELD,
                                   dm.COW_ESD04_YIELD
                              FROM CHIPDM.DM_SMP_PROBER dm,
                                   (SELECT lot,
                                           wo,
                                           COMPONENTID,
                                           STRUCTCODE,
                                           SUBSTRUCTCODE
                                      FROM epi.MES_EPI_WO_BOOK,
                                           epi.MES_WIP_COMP_CREATE
                                     WHERE epi.MES_EPI_WO_BOOK.lot =
                                              epi.MES_WIP_COMP_CREATE.CREATELOT)
                                   b,
                                   mes_wms_lot_nonactive wms
                             WHERE     dm.epicomponentid = wms.waferid
                                   AND dm.COW_STARTTIME IS NOT NULL
                                   AND dm.epicomponentid = b.COMPONENTID
                                   AND dm.COW_LOP2_AVG <> 0
                                   AND dm.COW_LOP2_AVG IS NOT NULL
                                   AND SUBSTR (dm.COW_STARTTIME, 1, 10) >=
                                          ‘2019/12/09‘
                                   AND SUBSTR (dm.COW_STARTTIME, 1, 10) <=
                                          ‘2020/01/02‘
                                   AND SUBSTR (dm.lot, 3, 6) IN (‘W1028K‘)
                                   AND SUBSTR (b.COMPONENTID, 12, 1) IN (‘L‘,
                                                                         ‘P‘,
                                                                         ‘Y‘)
                                   AND SUBSTR (dm.LOT, 2, 1) IN (‘H‘,
                                                                 ‘I‘,
                                                                 ‘K‘,
                                                                 ‘L‘)
                                   AND b.STRUCTCODE IN (‘W4L2D06‘, ‘W4L2D05‘)
                                   AND b.SUBSTRUCTCODE IN (‘01‘,
                                                           ‘02‘,
                                                           ‘03‘,
                                                           ‘04‘))
                  GROUP BY STRUCTCODE, SUBSTRUCTCODE, time_test
                  ORDER BY time_test) UNPIVOT (VALUE
                                      FOR category
                                      IN  (片数,
                                          COW_LOP1_AVG_K,
                                          SMP_LOP1,
                                          SMP_LOP2,
                                          SMP_VF1,
                                          SMP_WLD1,
                                          MM400,
                                          --BS,
                                          SMP_VF4,
                                          SMP_HW1,
                                          SMP_IR1_YIELD,
                                          SMP_VZ1,
                                          ESD01_YIELD,
                                          --ESD03_YIELD,
                                          Droop))) PIVOT (MAX (aa)
                                                   FOR time1
                                                   IN  (‘2019/12/09‘ AS "2019/12/09",
                                                       ‘2019/12/10‘ AS "2019/12/10",
                                                       ‘2019/12/11‘ AS "2019/12/11",
                                                       ‘2019/12/12‘ AS "2019/12/12",
                                                       ‘2019/12/13‘ AS "2019/12/13",
                                                       ‘2019/12/14‘ AS "2019/12/14",
                                                       ‘2019/12/15‘ AS "2019/12/15",
                                                       ‘2019/12/16‘ AS "2019/12/16",
                                                       ‘2019/12/17‘ AS "2019/12/17",
                                                       ‘2019/12/18‘ AS "2019/12/18",
                                                       ‘2019/12/19‘ AS "2019/12/19",
                                                       ‘2019/12/20‘ AS "2019/12/20",
                                                       ‘2019/12/21‘ AS "2019/12/21",
                                                       ‘2019/12/22‘ AS "2019/12/22",
                                                       ‘2019/12/23‘ AS "2019/12/23",
                                                       ‘2019/12/24‘ AS "2019/12/24",
                                                       ‘2019/12/25‘ AS "2019/12/25",
                                                       ‘2019/12/26‘ AS "2019/12/26",
                                                       ‘2019/12/27‘ AS "2019/12/27",
                                                       ‘2019/12/28‘ AS "2019/12/28",
                                                       ‘2019/12/29‘ AS "2019/12/29",
                                                       ‘2019/12/30‘ AS "2019/12/30",
                                                       ‘2019/12/31‘ AS "2019/12/31",
                                                       ‘2020/01/01‘ AS "2020/01/01",
                                                       ‘2020/01/02‘ AS "2020/01/02"))
ORDER BY ss,
         DECODE (category,
                 ‘片数‘, 1,
                 ‘COW_LOP1_AVG_K‘, 2,
                 ‘SMP_LOP1‘, 3,
                 ‘SMP_LOP2‘, 4,
                 ‘SMP_VF1‘, 5,
                 ‘SMP_WLD1‘, 6,
                 ‘MM400‘, 7,
                 ‘SMP_VF4‘, 8,
                 ‘SMP_HW1‘, 9,
                 ‘SMP_IR1_YIELD‘, 10,
                 ‘SMP_VZ1‘, 11,
                 ‘ESD01_YIELD‘, 12,
                 ‘Droop‘, 13) ASC

只要你Ctrl+C  Ctrl+V 放到对应的语法中 就能实现

  SELECT *
    FROM (SELECT time1,--根据时间排序
                 structcode || ‘-‘ || substructcode ss,--不做转换的
                 CASE
                    WHEN category = ‘片数‘ THEN ‘片数‘
                    WHEN category = ‘COW_LOP1_AVG_K‘ THEN ‘COW_LOP1_AVG_K‘
               --你想转哪个字段就填哪个字段 填了的下面 UNPIVOT (VALUE FOR category IN  (这里面就得填)
                 END
                    category,VALUE AS aa --aa只是个自定义名称
            FROM (  

                       --放入你的表sql

                  ) UNPIVOT (VALUE FOR category
                                      IN  (片数,
                                          COW_LOP1_AVG_K -- category列的数据 字段名称数量要和case when一样
                                            ))) PIVOT (MAX (aa) FOR time1
                                                   IN  (‘2019/12/09‘ AS "2019/12/09",
                                                       ‘2019/12/10‘ AS "2019/12/10",
                                                       ‘2019/12/11‘ AS "2019/12/11"
                                                        ))--你的列
ORDER BY ss,--排序不要在意
         DECODE (category,
                 ‘片数‘, 1,
                 ‘COW_LOP1_AVG_K‘, 2) ASC--自定义排序 可有可无

原文地址:https://www.cnblogs.com/lin02/p/12204835.html

时间: 2025-01-17 21:42:11

oracle 格式转换 <行转列列转行>的相关文章

Oracle 多行变一列的方法

多行变一列的方法有很多,觉得这个第一眼看懂了当时就用的这个办法. 情况是这样的.以下数据前几列是一样的,需要把VAT_VALUE_CHAR 的值放在同一行上. SELECT * FROM ps_vat_defaults defaults WHERE defaults.vat_driver = 'VAT_ENT_RGSTRN' AND defaults.vat_driver_key1 = 'AMB19' AND defaults.vat_driver_key2 = 'DEU' AND vat_de

Sql server 中将数据行转列列转行(二)

老规矩,先弄一波测试数据,数据填充代码没有什么意义,先折叠起来: /* 第一步:创建临时表结构 */ CREATE TABLE #Student --创建临时表 ( StuName nvarchar(20), --学生名称 Chinese int, Math int, English int ) DROP TABLE #Student --删除临时表 SELECT * FROM #Student --查询所有数据 INSERT INTO #Student(StuName,Chinese,Math

Oracle 多行转多列

Oracle 多行转多列,列值转为列名 前段时间做调查问卷,客户创建自定义问卷内容,包括题目和选项内容; 之后需要到处问卷明细,,,,麻烦来咯 于是到网上到处搜索,没有直接结果;于是又找各种相似的,,终于功夫不负有心人 然后最终自己写出来了,decode才是核心 废话不多说,看图 需求示例图表: 存储过程,嘿嘿: 1 create or replace procedure NAG_QUESTIONERSULT_EXP( 2 V_QID in number, 3 C_Title out sys_r

数据库 行转列 列转行详解

目录结构如下: 行转列 列转行 [一].行转列 1.1.初始测试数据 表结构:TEST_TB_GRADE create table TEST_TB_GRADE ( ID        NUMBER(10) not null, USER_NAME VARCHAR2(20 CHAR), COURSE    VARCHAR2(20 CHAR), SCORE     FLOAT ) [sql] view plaincopy create table TEST_TB_GRADE ( ID        N

MSSQLServer 纵向表转横向表 横向表转纵向表 行转列 列转行

MSSQLServer 纵向表转横向表  横向表转纵向表 建表语句及插入数据语句: CREATE TABLE Test_y( [Name] [nchar](10) NULL, [Course] [nchar](10) NULL, [Grade] [int] NULL ) insert into Test_y values ('张三','语文',75); insert into Test_y values ('张三','数学',80); insert into Test_y values ('张三

oracle 多行转一列,一列转多行

oracle 多行转一列,如下: select to_char(wmsys.wm_concat(t.candi_supplier_id))   from PS.T_PS_BU_AWARD_SUPPLIER t; 通过oracle 10g 提供的wmsys.wm_concat函数,即可以完成行转列的效果. 一列转多行,如下: with a as (select '/ABC/AA/AD/ABD/JI/CC/ALSKD/ALDKDJ' id from dual)select regexp_substr

html5的行标签与列标签

html5的标签很多,这里不一一列举.下面只将常用的一些html5的行标签和列标签进行了分类总结,方便大家学习借鉴.注意:一些标签可在css样式中进行行标签与列标签的互相转换,这里所列举的标签没有进行任何样式的设置,所有列标签和行标签经本人亲测有效.块标签: <h1>一级标题 <h2>二级标题 <h3>三级标题 <h4>四级标题 <h5>五级标题 <h6>六级标题 <ul>无序列表 <ol>有序列表 <d

地瓜君的excel小课堂——行函数与列函数

大家用Excel处理数据的时候经常会用到一些查找引用函数,今天地瓜君就来和大家一起学习一下查找引用函数中的行函数与列函数啦. 导航:1.row函数 2.rows函数 3.column函数 4.columns函数 我们重点介绍这4个函数里最常见也是非常实用的函数row函数,其他3个函数不是最常用所以就稍微简单的介绍下啦 一.row函数(返回一个引用的行号) 语法:=row(reference) 说明:如果省略reference(参数),则默认返回row函数所在单元格的行号. 参数reference

行数据库VS列数据库

一.介绍 目前大数据存储有两种方案可供选择:行存储和列存储.业界对两种存储方案有很多争持,集中焦点是:谁能够更有效地处理海量数据,且兼顾安全.可靠.完整性.从目前发展情况看,关系数据库已经不适应这种巨大的存储量和计算要求,基本是淘汰出局.在已知的几种大数据处理软件中,Hadoop的HBase采用列存储,MongoDB是文档型的行存储,Lexst是二进制型的行存储. 顾名思义,这两种数据库架构在存贮数据时的方式是大相径庭的.在行式数据库中,每一行中的每一块数据都是紧挨着另一块数据存放在硬盘中.一般