【商业智能图表superset】数据源行变为列
目录
场景是数据源按月计算的,但是想按1号,2号,3号,的列来查看,这里要先汇总,按姓名group by,将行转换为列,数据源不同时,对时间的函数不一样。
1、数据源为posgresql
postgres的抽取时间中月的第几天:extract(day from update_time)
SELECT person_no AS person_no,
org_name AS org_name,
extract(day from update_time),
sum(case when extract(day from update_time) =1 then quota else 0 end) as day1,
sum(case when extract(day from update_time) =2 then quota else 0 end) as day2,
case when sum(case when extract(day from update_time) =1 then quota else 0 end) =3 then 'gd' else '' end as day1_str
FROM public.tb_quota
WHERE update_time >= '2020-08-31 00:00:00.000000'
AND update_time < '2020-09-07 00:00:00.000000'
group by person_no,org_name,update_time
LIMIT 1000;```
### 2、数据源为clickhouse
clickhouse的抽取时间中月的第几天:toDayOfMonth(date)
```bash
SELECT formatDateTime(date,'%Y-%m') AS month_str,
person_name AS person_name,
person_id AS person_id,
sum(case
when toDayOfMonth(date) =10 then state
else 0
end) AS day10,
sum(case
when toDayOfMonth(date) =1 then state
else 0
end) AS "1",
sum(case
when toDayOfMonth(date) =11 then state
else 0
end) AS "11"
FROM pdms_pdmsdb.tb_model
GROUP BY formatDateTime(date,'%Y-%m'),
person_name,
person_id
ORDER BY day10 DESC
LIMIT 1000;```
如果要先判断是否存在,然后再转义,单个列需要这样:
```python
case when has(groupArray(toDayOfMonth(date)),25)=0 then '' when sum(case when toDayOfMonth(date) =25 then (case when attendance_state is NULL then 100 else attendance_state end) else 0 end)=0 then '正常'
when sum(case when toDayOfMonth(date) =25 then (case when attendance_state is NULL then 100 else attendance_state end) else 0 end)=1 then '迟到'
when sum(case when toDayOfMonth(date) =25 then (case when attendance_state is NULL then 100 else attendance_state end) else 0 end)=2 then '早退'
when sum(case when toDayOfMonth(date) =25 then (case when attendance_state is NULL then 100 else attendance_state end) else 0 end)=3 then '旷工'
when sum(case when toDayOfMonth(date) =25 then (case when attendance_state is NULL then 100 else attendance_state end) else 0 end)=4 then '请假'
when sum(case when toDayOfMonth(date) =25 then (case when attendance_state is NULL then 100 else attendance_state end) else 0 end)=100 then '未排班'
else '' end```

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)