简体   繁体   English

pivot一组资料汇总

[英]pivot a group data summary

I have the following table in sql and I need to use data summary我在sql有下表需要用到数据汇总

DEPARMENT部门 JOB工作 QUARTER四分之一 ITEM物品
Training训练 null null Q1 Q1 8 8个
Support支持 null null Q2 Q2 4 4个
Support支持 null null Q3 Q3 2 2个
Research and Development研究与开发 null null Q4第四季度 2 2个
Support支持 Account Coordinator客户协调员 Q1 Q1 8 8个
Training训练 Account Coordinator客户协调员 Q2 Q2 5 5个
Engineering工程 Account Coordinator客户协调员 Q3 Q3 1 1个
Support支持 Account Coordinator客户协调员 Q4第四季度 2 2个
Services服务 Account Executive业务经理 Q1 Q1 5 5个
Support支持 Account Executive业务经理 Q2 Q2 4 4个
Support支持 Account Executive业务经理 Q3 Q3 5 5个
Support支持 Account Executive业务经理 Q4第四季度 7 7
Human Resources人力资源 Account Representative I一级客户代表 Q1 Q1 1 1个
Services服务 Account Representative III三级客户代表 N/I 1 1个
Research and Development研究与开发 Account Representative III三级客户代表 Q1 Q1 2 2个
Business Development业务发展 Account Representative III三级客户代表 Q2 Q2 1 1个
Support支持 Account Representative III三级客户代表 Q4第四季度 2 2个
Training训练 Account Representative IV四级客户代表 Q1 Q1 4 4个
Support支持 Account Representative IV四级客户代表 Q2 Q2 3 3个
Support支持 Account Representative IV四级客户代表 Q4第四季度 5 5个
Training训练 Accountant I一级会计师 Q1 Q1 6 6个
Product Management产品管理 Accountant I一级会计师 Q2 Q2 1 1个
Marketing营销 Accountant I一级会计师 Q3 Q3 5 5个

I have tried to use but it does not bring me the desired results我试过使用但它没有给我带来想要的结果

SELECT DEPARMENT
    , MAX(JOB)
    , COUNT(QUARTER)
FROM TESTHIRED_EMPLOYEE
GROUP BY DEPARMENT
    , JOB
    , QUARTER

To obtain a table like this获得这样的表格

DEPARMENT部门 JOB工作 Q1 Q1 Q2 Q2 Q3 Q3 Q4第四季度
Support支持 Account Coordinator客户协调员 30 30 0 0 10 10 5 5个
Services服务 Account Representative III三级客户代表 0 0 2 2个 3 3个 1 1个
Research and Development研究与开发 Account Representative III三级客户代表 0 0 0 0 12 12
Business Development业务发展 Account Representative III三级客户代表 10 10 0 0 0 0 2 2个
Support支持 Account Representative III三级客户代表 23 23 2 2个 1 1个 6 6个

This documentation should explain a bit: https://learn.microsoft.com/en-us/azure/databricks/sql/language-manual/sql-ref-syntax-qry-select-pivot .该文档应该解释一下: https://learn.microsoft.com/en-us/azure/databricks/sql/language-manual/sql-ref-syntax-qry-select-pivot It also has solutions without a pivot with the use of, in your case, count(qarter) filter(where quarter = 'Q1') as Q1它也有没有 pivot 的解决方案,在您的情况下使用count(qarter) filter(where quarter = 'Q1') as Q1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM