piv

SQL SERVER 2005+ 行列互转函数 PIVOT和UNPIVOT

淺唱寂寞╮ 提交于 2020-03-19 06:32:37
PIVOT 用于将列值旋转为列名(即行转列),UNPIVOT 正好相反 MSDN: http://technet.microsoft.com/zh-cn/library/ms177410.aspx 此函数2005以上版本才有的。 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>) ) create table #tb(姓名 varchar(10),课程 varchar(10),分数 int) insert into #tb values('张三','语文',74) insert into #tb values('张三','数学',83) insert into #tb values('张三','物理',93) insert into #tb values('李四','语文',74)

Oracle数据库中某个表中的某个字段的值是用逗号隔开的多个值,根据逗号拆分并从关联表中查出数据返回

安稳与你 提交于 2020-01-16 14:28:43
工作中遇到一个问题,表A中有一个字段,存的是编码值,但是不规范,存的多个值,用逗号(既有英文逗号,又有中文逗号,还有空格)隔开了;现需要根据编码值关联对应的编码表查出编码名称,所以网上查找资料得出了解决方法。本人用的是Oracle数据库。 参考MySQL数据库解决方法:https://blog.csdn.net/myme95/article/details/82661661 一、创建测试数据表 1.bi_test表(course_id既有英文逗号,又有中文逗号,还有空格) 2.bm_test表 3.希望得到的结果: (1) (2) 二、使用的函数 1.MySQL数据库可以直接使用FIND_IN_SET函数和group_concat()函数实现。详情戳https://blog.csdn.net/myme95/article/details/82661661 查看MySQL实现方法。 2.Oracle重写FIND_IN_SET函数(参考自:https://www.cnblogs.com/Iqiaoxun/p/5606639.html) CREATE OR REPLACE FUNCTION FIND_IN_SET(piv_str1 varchar2, piv_str2 varchar2, p_sep varchar2 := ',',p_sep2 varchar2 := ',')

python使用pandas创建透视表(Pivot tables)

孤街醉人 提交于 2019-12-03 20:28:26
代码在最下面 思路: 1、准备号DataFrame数据集 2、根据需要将DataFrame转换成透视表    2.1、创建简单的透视表(默认计算平均值)    2.2、修改参数,满足需求(求和,计算数量等) 本文参考链接参考:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot_table.html?highlight=pivot_table#pandas.DataFrame.pivot_table 1、准备DataFrame    通过代码生成一个 24行,5列 的DataFrame,D和E是长度为24,具有标准正态分布的一维数组    生成了一个24行5列的DataFrame,格式如下 2、根据需要将DataFrame转换成透视表   2.1、 创建一个简单的透视表,格式为:A作为行,计算D列的平均值        生成的透视表结果如下    2.2、修改参数      创建一个简单的透视表,格式为:A,作为行,计算D列的总和        生成的透视表结果如下      具体代码如下 import pandas as pd import numpy as np import datetime #通过代码生成一个 24行,5列 的DataFrame,D和E是长度为24

oracle 实现mysql find_set_in函数

こ雲淡風輕ζ 提交于 2019-12-03 16:45:28
create or replace FUNCTION F_FIND_IN_SET(piv_str1 varchar2, piv_str2 varchar2, p_sep varchar2 := ',') RETURN NUMBER IS l_idx number:=0; -- 用于计算piv_str2中分隔符的位置 str varchar2(500); -- 根据分隔符截取的子字符串 piv_str varchar2(500) := piv_str2; -- 将piv_str2赋值给piv_str res number:=0; -- 返回结果 res_place number:=0;-- 原字符串在目标字符串中的位置 BEGIN -- 如果字段是null 则返回0 IF piv_str2 IS NULL THEN RETURN res; END IF; -- 如果piv_str中没有分割符,直接判断piv_str1和piv_str是否相等,相等 res_place=1 IF instr(piv_str, p_sep, 1) = 0 THEN IF piv_str = piv_str1 THEN res_place:=1; res:= res_place; END IF; ELSE -- 循环按分隔符截取piv_str LOOP l_idx := instr(piv_str,p

Flatten DataFrame with multi-index columns

匿名 (未验证) 提交于 2019-12-03 02:52:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'd like to convert a Pandas DataFrame that is derived from a pivot table into a row representation as shown below. This is where I'm at: import pandas as pd import numpy as np df = pd.DataFrame({ 'goods': ['a', 'a', 'b', 'b', 'b'], 'stock': [5, 10, 30, 40, 10], 'category': ['c1', 'c2', 'c1', 'c2', 'c1'], 'date': pd.to_datetime(['2014-01-01', '2014-02-01', '2014-01-06', '2014-02-09', '2014-03-09']) }) # we don't care about year in this example df['month'] = df['date'].map(lambda x: x.month) piv = df.pivot_table(["stock"], "month", ["goods",

Type mismatch error when creating a pivot table in Excel with VBA

匿名 (未验证) 提交于 2019-12-03 01:39:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm trying to put a macro together that will make a simple pivot table using the data from an active worksheet. When I try to run it, I receive a type mismatch error. When I start the debugger, the first section is highlighted: ActiveWorkbook.PivotCaches through xlPivotTableVersion10. Initially, the TableDestination was blank and I thought that might be the problem, but after adding a destination I still get the same error. ActiveWorkbook . PivotCaches . Add ( SourceType := xlDatabase , _ SourceData := ActiveSheet . UsedRange ).

oracle 实现mysql find_set_in函数

匿名 (未验证) 提交于 2019-12-02 22:06:11
create or replace FUNCTION F_FIND_IN_SET(piv_str1 varchar2, piv_str2 varchar2, p_sep varchar2 := ',') RETURN NUMBER IS BEGIN -- 如果字段是null 则返回0 IF piv_str2 IS NULL THEN END IF; -- 如果piv_str中没有分割符,直接判断piv_str1和piv_str是否相等,相等 res_place=1 IF instr(piv_str, p_sep, 1) = 0 THEN ELSE LOOP END F_FIND_IN_SET; 来源:博客园 作者: vofill-work 链接:https://www.cnblogs.com/vofill/p/11803137.html