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不支持这两个函数,所以需要自己来完成。

原文地址:https://www.cnblogs.com/neughj/p/4958296.html