Mysql 行列转换
一、第一种
原数据表
转换后
DROP TABLE IF EXISTS tempdynamic;
CREATE TEMPORARY TABLE tempdynamic (
SELECT p.fsPaymentName,sr.fsPaymentId,sh.fsShiftName,SUM(sr.fdReceMoney) as AmtTotal FROM tbsell as s
INNER JOIN tbSellReceive as sr ON s.fsSellNo=sr.fsSellNo AND s.fsShopGUID=sr.fsShopGUID
INNER JOIN tbpayment as p on sr.fsPaymentId =p.fsPaymentId and p.fsShopGUID=s.fsShopGUID and p.fiStatus=1
INNER JOIN tbSellCheck as sc on sc.fsSellNo=s.fsSellNo AND s.fsShopGUID=sc.fsShopGUID
INNER JOIN tbshift as sh ON sc.fsShiftId=sh.fsShiftId AND sh.fiStatus=1
WHERE s.fiBillStatus=3
AND s.fsShopGUID='c5f0d5c9d8b349f493c180bcc22d9ce0'
AND s.fsSellDate='2016-01-15'
GROUP BY sr.fsPaymentId,sc.fsShiftId); SET @EE='';
set @str_tmp=''; SELECT @EE:=CONCAT(@EE,'SUM(IF(fsShiftName=\'',fsShiftName,'\'',',AmtTotal,0)) AS ',fsShiftName,',') as aa into @str_tmp
FROM (
SELECT DISTINCT fsShiftName from tempdynamic
) A order by length(aa) desc limit 1; SET @QQ=CONCAT('SELECT ifnull(fsPaymentName,\'total\') as 科目名称 ,',@str_tmp,' SUM(AmtTotal) as 合计 FROM tempdynamic Group by fsPaymentName WITH ROLLUP'); PREPARE stmt FROM @QQ;
EXECUTE stmt ;
deallocate prepare stmt;
二、第二种
原数据表
转换后
DROP TABLE IF EXISTS tempdynamic;
CREATE TABLE tempdynamic (
SELECT sh.fsShiftName,sh.fsShiftId, SUM(s.fdSaleAmt) as 销售金额,SUM(s.fdDiscountAmt) as 折扣金额, SUM(s.fdServiceAmt) as 服务费金额,SUM(s.fdExpAmt) as 应收金额 ,SUM(s.fdRealAmt) as 实收金额
FROM tbsell as s
INNER JOIN tbSellCheck as sc on sc.fsSellNo=s.fsSellNo AND s.fsShopGUID=sc.fsShopGUID
INNER JOIN tbshift as sh ON sc.fsShiftId=sh.fsShiftId AND sh.fiStatus=1
WHERE s.fiBillStatus=3
AND s.fsShopGUID='c5f0d5c9d8b349f493c180bcc22d9ce0'
AND s.fsSellDate='2016-01-15'
GROUP BY sc.fsShiftId); DROP TABLE IF EXISTS tempdynamica;
CREATE TEMPORARY TABLE tempdynamica(
SELECT a.* FROM (
SELECT fsShiftName,'销售金额' AS 科目,销售金额 AS AmtTotal FROM tempdynamic UNION ALL
SELECT fsShiftName,'折扣金额' AS 科目,折扣金额 AS AmtTotal FROM tempdynamic UNION ALL
SELECT fsShiftName,'服务费金额' AS 科目,服务费金额 AS AmtTotal FROM tempdynamic UNION ALL
SELECT fsShiftName,'应收金额' AS 科目,应收金额 AS AmtTotal FROM tempdynamic UNION ALL
SELECT fsShiftName,'实收金额' AS 科目,实收金额 AS AmtTotal FROM tempdynamic
ORDER BY fsShiftName,科目 DESC) as a ); SET @EE='';
set @str_tmp='';
SELECT @EE:=CONCAT(@EE,'SUM(IF(fsShiftName=\'',fsShiftName,'\'',',AmtTotal ,0)) AS ',fsShiftName,',') as aa into @str_tmp
FROM (
SELECT DISTINCT fsShiftName from tempdynamic
) A order by length(aa) desc limit 1;
SET @QQ=CONCAT('SELECT ifnull(科目,\'total\') as 科目名称,',@str_tmp,'sum(AmtTotal) as 合计 FROM tempdynamica Group by 科目 WITH ROLLUP');
PREPARE stmt FROM @QQ;
EXECUTE stmt ;
deallocate prepare stmt;
postgresql
原数据:
转换后:
sql:
CREATE OR REPLACE FUNCTION "public"."test"()
RETURNS "pg_catalog"."void" AS $BODY$
DECLARE
dynsql1 VARCHAR ;
dynsql2 VARCHAR ;
dynsql3 VARCHAR ;
BEGIN
drop table if exists temp_sell;
dynsql1='CREATE TEMP TABLE temp_sell AS SELECT
bd.area_id,
bd.area_name,
bd.sell_date,
SUM (
COALESCE (bss.original_amt, 0)
) AS original_amt
FROM
bill_detail AS bd
INNER JOIN bill_settlement_summary AS bss ON bd.bill_id = bss.bill_id
WHERE
bd.sell_date BETWEEN ''2018-01-01''
AND ''2018-01-30''
AND bd.shop_guid=''143174''
-- AND area_id=''''
-- AND msection_id=''''
GROUP BY
bd.sell_date,
bd.area_id,
bd.area_name;';
EXECUTE dynsql1;
dynsql2 = '';
dynsql3 = '';
drop table if exists temp_sell_1;
CREATE TEMP TABLE temp_sell_1 AS
SELECT concat('SUM(CASE WHEN sell_date=''',sell_date,'''','then original_amt else 0 end) AS "',sell_date,'"') AS aa
FROM
(SELECT DISTINCT
sell_date
FROM
temp_sell) A
ORDER BY sell_date ASC ;
SELECT string_agg(aa, ',') INTO dynsql2 from temp_sell_1;
RAISE NOTICE 'dynsql2==[%]', dynsql2 ;
dynsql3 = CONCAT(
'SELECT area_id as 区域id,area_name as 区域名称,' ,dynsql2,
',sum(original_amt) 汇总 FROM temp_sell Group By area_id,area_name;'
);
RAISE NOTICE 'dynsql3==[%]', dynsql3 ;
END ;
$BODY$
LANGUAGE 'plpgsql' VOLATILE COST 100
;
SELECT test();
SELECT area_id as 区域id,area_name as 区域名称,SUM(CASE WHEN sell_date='2018-01-12'then original_amt else 0 end) AS "2018-01-12",SUM(CASE WHEN sell_date='2018-01-18'then original_amt else 0 end) AS "2018-01-18",SUM(CASE WHEN sell_date='2018-01-19'then original_amt else 0 end) AS "2018-01-19",SUM(CASE WHEN sell_date='2018-01-25'then original_amt else 0 end) AS "2018-01-25",sum(original_amt) 汇总 FROM temp_sell Group By area_id,area_name;
第二种方案:
参考: http://www.cureffi.org/2013/03/19/automatically-creating-pivot-table-column-names-in-postgresql/
使用函数如下:
CREATE OR REPLACE FUNCTION "public"."pivotcode"("tablename" varchar, "rowc" varchar, "rowctype" varchar, "groupby" varchar, "colc" varchar, "cellc" varchar, "celldatatype" varchar)
RETURNS "pg_catalog"."void" AS $BODY$
DECLARE rec record ; dynsql1 VARCHAR ; dynsql2 VARCHAR ; countrows INT; columnlist VARCHAR ;
BEGIN
drop table if exists temp_sell_result;
dynsql1 = 'select count(1) from ' || tablename || ';' ;
EXECUTE dynsql1 INTO countrows ;
IF countrows=0 THEN
dynsql1='CREATE TEMP TABLE temp_sell_result AS select * from ' || tablename || ';';
EXECUTE dynsql1;
ELSE
-- 1. retrieve list of column names.
dynsql1 = 'select string_agg(distinct ''"''||' || colc || '||''" ' || celldatatype || ''','','' order by ''"''||' || colc || '||''" ' || celldatatype || ''') from ' || tablename || ';' ;
--RAISE NOTICE '%', dynsql1 ;
EXECUTE dynsql1 INTO columnlist ;
-- 2. set up the crosstab query dynsql2 = 'CREATE TEMP TABLE temp_sell_result AS select * from crosstab (
''select ' || rowc || ',' || colc || ',' || cellc || ' from ' || tablename || ' group by '||groupby||' order by '||groupby||''',
''select distinct ' || colc || ' from ' || tablename || ' order by 1''
)
as newtable (
' || rowctype || ',' || columnlist || '
);' ;
EXECUTE dynsql2 ;
END IF;
END ;
$BODY$
LANGUAGE 'plpgsql' VOLATILE COST 100
; ALTER FUNCTION "public"."pivotcode"("tablename" varchar, "rowc" varchar, "rowctype" varchar, "groupby" varchar, "colc" varchar, "cellc" varchar, "celldatatype" varchar) OWNER TO "winposdev";
Mysql 行列转换的更多相关文章
- 【学亮IT手记】MySql行列转换案例
create table score( name ), math int, english int ); ,); ,); ,); ,); SHOW tables; SELECT * from scor ...
- MySQL行列转换
分类: Mysql/postgreSQL 在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解决方法,特发贴集思广义.http://topic.csdn.net/u ...
- MySQL行列转换拼接
mysql> select TBL_ID,CREATE_TIME,LAST_ACCESS_TIME,TBL_NAME,TBL_TYPE from TBLS; +--------+-------- ...
- 【转载】mysql行列转换方法总结
[转载]mysql行列转换方法总结 [MySQL] 行列转换变化各种方法实现总结(行变列报表统计.列变行数据记录统计等) Mysql 列转行统计查询 .行转列统计查询 在某些数据库中有交叉表,但在My ...
- Mysql实现行列转换
前言: 最近又玩起了sql语句,想着想着便给自己出了一道题目:“行列转换”.起初瞎折腾了不少时间也上网参考了一些博文,不过大多数是采用oracle数据库当中的一些便捷函数进行处理,比如”pivot”. ...
- mysql:sql行列转换
今天一个同学遇到一个问题问我了,由于本人平时学习的mysql比较基础,确实没解决,后来google了一下,才知道是sql的一种技法[行列转换],话不多说先上图: 想得到下面的结果: +------+- ...
- MySQL中行列转换的SQL技巧
行列转换常见场景 由于很多业务表因为历史原因或者性能原因,都使用了违反第一范式的设计模式.即同一个列中存储了多个属性值(具体结构见下表). 这种模式下,应用常常需要将这个列依据分隔符进行分割,并得到列 ...
- mysql 行列动态转换(列联表,交叉表)
mysql 行列动态转换(列联表,交叉表) (1)动态,适用于列不确定情况 create table table_name( id int primary key, col1 char(2), col ...
- 简单的叙述下SQL中行列转换的小知识!
行列转换对于工作还是学习中总是不可避免的会遇到(虽然本人还尚未工作,萌萌哒的学生一枚),解决的方法也有很多,我这里就总结一下我所想解决的问题以及怎么去解决的方法, 可能网上已经有很多类似的方法了,有的 ...
随机推荐
- php7 安装redis扩展
从pecl下载的phpredis扩展在make时报错,google了一下,解决办法,因为phpredis 有专门为php7的分支,所以先git clone phpredis下来 git clone h ...
- ATM+购物车
本次代码的实现用到了模块间的互相调用,这对于初学者而言有些难度,不过这也是我们必须要掌握的,在以后的大程序中,多个模块会让我们的代码看起来简洁明了,易排错 (本次代码只是简单的实现的基本的功能,其代码 ...
- AngularJS 动画
AngularJS 提供了动画效果,可以配合 CSS 使用. AngularJS 使用动画需要引入 angular-animate.min.js 库. <script src="htt ...
- 文件上传之 HttpPostedFile
HttpPostedFile类,提供对客户端已上载的单独文件的访问. 公共属性如下: SaveAs()方法,用于保存上传文件的内容. 用法为: #region 文件上传 /// <summary ...
- 使用VBScript实现设置系统环境变量的小程序
本人有点桌面洁癖,桌面上只放很少的东西,很多软件都用快捷键调出.最近频繁用到一个软件,我又不想放个快捷方式在桌面,也不想附到开始菜单,于是乎想将其所在目录附加到系统环境变量Path上,以后直接在运行中 ...
- 爬虫requests模块 2
会话对象¶ 会话对象让你能够跨请求保持某些参数.它也会在同一个 Session 实例发出的所有请求之间保持 cookie, 期间使用 urllib3 的 connection pooling 功能.所 ...
- Linux内核源代码获取教程
Linux内核源代码获取方法 什么叫Linux 什么叫Linux内核 Linux内核源代码的获取 什么叫Linux? Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UN ...
- git客服端基本操作
以下操作基于git+gerrit 1.生成公钥 ssh -keygen -t rsa 默认公钥生成路径 C:\Documents and Settings\用户名\.ssh 2.配置姓名和邮箱地址 ...
- string,stringbuilder,stringbuffer用法
总结:1.如果要操作少量的数据用 = String ==================================>字符串常量2.单线程操作字符串缓冲区 下操作大量数据 = Strin ...
- 好文分享_java堆栈的区别
Java Heap Memory Heap memory is used by java runtime to allocate memory to Objects and JRE classes. ...