zoukankan      html  css  js  c++  java
  • MSSQL中 ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)在Mysql中的实现

    之前的MS SQL 中存储过程有下面一段代码:

    with rowvalue as
                (
                    SELECT ROW_NUMBER() OVER (ORDER BY F_WarnName DESC) AS id,
                    F_PID,F_WarnName,Sub,Result
                    FROM   
                   (SELECT F_PID,F_WarnName,F_Ratio00,F_Ratio01,F_Ratio02,
                   F_Ratio03,F_Ratio04,F_Ratio05,F_Ratio06,F_Ratio07,F_Ratio08,
                   F_Ratio09,F_Ratio10,F_Ratio11,F_Ratio12,F_Ratio13,F_Ratio14,F_Ratio15,
                   F_Ratio16,F_Ratio17,F_Ratio18,F_Ratio19,F_Ratio20,F_Ratio21,F_Ratio22,F_Ratio23
                            FROM T_ZZ_ParaBehaviorWarn where F_PID=@Pid) p  
                    UNPIVOT  
                   (Result FOR Sub IN (F_Ratio00,F_Ratio01,F_Ratio02,
                   F_Ratio03,F_Ratio04,F_Ratio05,F_Ratio06,F_Ratio07,F_Ratio08,
                   F_Ratio09,F_Ratio10,F_Ratio11,F_Ratio12,F_Ratio13,F_Ratio14,F_Ratio15,
                   F_Ratio16,F_Ratio17,F_Ratio18,F_Ratio19,F_Ratio20,F_Ratio21,F_Ratio22,F_Ratio23))       
                    AS unpvt
                ),

    其中用到了 ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN),主要是实现分页的功能。

    而需要移植到MySql中,因为领导不让该程序里的代码,只能硬着头皮照搬了。

    下面是我在MySql中的实现:

    set @mycnt = 0;
          CREATE TEMPORARY TABLE IF NOT EXISTS rowvalue
          (
            SELECT  (@mycnt := @mycnt + 1) as id,
                    F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,Sub,Result
                    FROM   
                   (
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn00' AS Sub,F_EvalWarn00 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
               UNION ALL
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn01' AS Sub,F_EvalWarn01 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
               UNION ALL
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn02' AS Sub,F_EvalWarn03 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
               UNION ALL
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn03' AS Sub,F_EvalWarn03 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
               UNION ALL
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn04' AS Sub,F_EvalWarn04 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
               UNION ALL
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn05' AS Sub,F_EvalWarn05 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
               UNION ALL
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn06' AS Sub,F_EvalWarn06 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
               UNION ALL
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn07' AS Sub,F_EvalWarn07 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
               UNION ALL
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn08' AS Sub,F_EvalWarn08 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
               UNION ALL
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn09' AS Sub,F_EvalWarn09 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
               UNION ALL
               SELECT F_PID,F_WarnName,F_Interval,F_StartDate,F_EndDate,F_State,'F_EvalWarn10' AS Sub,F_EvalWarn10 AS Result FROM T_ZZ_ParaEvalWarn where F_PID=P_Pid and F_Interval=P_ReportType
             ) AS T ORDER BY F_WarnName DESC
    
          );

    顺表也实现了MS SQL 中行列转换中UNPIVOT在MySql中的实现:

    首先介绍一下MS SQL中行列转换函数的概念和格式:

    PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现

    PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P

    完整语法:

    table_source

    PIVOT(

    聚合函数(value_column)

    FOR pivot_column

    IN(<column_list>)

    )

    UNPIVOT用于将列明转为列值(即列转行),在SQL Server 2000可以用UNION来实现

    完整语法:

    table_source

    UNPIVOT(

    value_column

    FOR pivot_column

    IN(<column_list>)

    )

    而MySql不支持这两个函数,所以需要自己来完成。

  • 相关阅读:
    012_DRC检查与处理
    深度系统20.3中亿图图示任务栏名称显示乱码
    deepin20.3+nvidia460.27+cuda11.2+cudnn8.1.1+anconda3.2021.11+paddle2.1.2
    C++中使用DOM写XML文档
    理解lvalue和rvalue
    C++/CLI与C#常用语法对比
    VC++ MSXML创建XML文件以及对XML文档解析
    Stack overflow 编译能通过,运行时出现Stack overflow
    于typedef的用法总结
    VC2008操作Excel2007总结
  • 原文地址:https://www.cnblogs.com/neughj/p/4958296.html
Copyright © 2011-2022 走看看