-
-
wuq 初学数据Lv2
发表于2024-11-22 19:25
楼主
本帖最后由 wuq 于 2024-11-22 19:28 编辑
你是否遇到过用i做成固定表,ABI要对i的数据按浮动维进行筛选过滤分析的情况?
需求分析:i做成固定表,数据库则存储的是一张大宽表,相当于一户一条数据,ABI展现需要按维度分析,则要求主题表的数据按产品字段(维度)分为多条数据,即按维度字段列转行多指标数据。
示例如下:i的报表
数据库表
主题表要求字段:
方法:sql列转行,通过sql对每个指标进行列转行,然后union all连接,再group by分组,sql写完后,通过数据整合模块做ETL数据处理。
一、通用sql参考附件示例(建表、插入数据、列转行完整示例)
二、oracle、达梦、mysql可用UNPIVOT函数,列转行sql具体参考如下:
select BBQ_ , USERID_,BTYPE_,SHTAG_ ,SHTAG2_, OPTION_ , C1,C2,cyfl, sum(yysr) yysr,sum(lrze) lrze
from(
SELECT BBQ_ , USERID_,BTYPE_,SHTAG_ ,SHTAG2_, OPTION_ , C1,C2,cyfl, yysr, 0 lrze
FROM IJDQD_B0
UNPIVOT
(
yysr FOR cyfl IN (
d7 as '11',
d8 as '12',
d9 as '14',
d10 as '13',
d11 as '15',
d13 as '21',
d14 as '22',
d15 as '30',
d16 as '40',
d17 as '50',
d18 as '60',
d19 as '70',
d20 as '80',
d21 as '90')
)
union all
SELECT BBQ_ , USERID_,BTYPE_,SHTAG_ ,SHTAG2_, OPTION_ , C1,C2,cyfl, 0 yysr, lrze
FROM IJDQD_B0
UNPIVOT
(
lrze FOR cyfl IN (
e7 as '11',
e8 as '12',
e9 as '14',
e10 as '13',
e11 as '15',
e13 as '21',
e14 as '22',
e15 as '30',
e16 as '40',
e17 as '50',
e18 as '60',
e19 as '70',
e20 as '80',
e21 as '90')
)
) group by BBQ_ , USERID_,BTYPE_,SHTAG_ ,SHTAG2_, OPTION_ , C1,C2,cyfl
最后,通过数据整合模块来抽取数据
列转行.rar
(528 Bytes, 下载次数: )
你是否遇到过用i做成固定表,ABI要对i的数据按浮动维进行筛选过滤分析的情况?
需求分析:i做成固定表,数据库则存储的是一张大宽表,相当于一户一条数据,ABI展现需要按维度分析,则要求主题表的数据按产品字段(维度)分为多条数据,即按维度字段列转行多指标数据。
示例如下:i的报表

数据库表

主题表要求字段:

方法:sql列转行,通过sql对每个指标进行列转行,然后union all连接,再group by分组,sql写完后,通过数据整合模块做ETL数据处理。
一、通用sql参考附件示例(建表、插入数据、列转行完整示例)
二、oracle、达梦、mysql可用UNPIVOT函数,列转行sql具体参考如下:
select BBQ_ , USERID_,BTYPE_,SHTAG_ ,SHTAG2_, OPTION_ , C1,C2,cyfl, sum(yysr) yysr,sum(lrze) lrze
from(
SELECT BBQ_ , USERID_,BTYPE_,SHTAG_ ,SHTAG2_, OPTION_ , C1,C2,cyfl, yysr, 0 lrze
FROM IJDQD_B0
UNPIVOT
(
yysr FOR cyfl IN (
d7 as '11',
d8 as '12',
d9 as '14',
d10 as '13',
d11 as '15',
d13 as '21',
d14 as '22',
d15 as '30',
d16 as '40',
d17 as '50',
d18 as '60',
d19 as '70',
d20 as '80',
d21 as '90')
)
union all
SELECT BBQ_ , USERID_,BTYPE_,SHTAG_ ,SHTAG2_, OPTION_ , C1,C2,cyfl, 0 yysr, lrze
FROM IJDQD_B0
UNPIVOT
(
lrze FOR cyfl IN (
e7 as '11',
e8 as '12',
e9 as '14',
e10 as '13',
e11 as '15',
e13 as '21',
e14 as '22',
e15 as '30',
e16 as '40',
e17 as '50',
e18 as '60',
e19 as '70',
e20 as '80',
e21 as '90')
)
) group by BBQ_ , USERID_,BTYPE_,SHTAG_ ,SHTAG2_, OPTION_ , C1,C2,cyfl
最后,通过数据整合模块来抽取数据


