使用databricks SQL创建了一个表,如下所示:
<表类>
表名
RunDate
质量
qaflag
状态
tbody><<tr>等等 2022-06-02 青铜 1 通过了 blah1 2022-06-02 银 1 失败 表类>
切换到distinct
的窗口函数以消除group by
的需求,然后使用case
表达式:
SELECT distinct Quality
, count(Status) over (partition by Quality) * 100 / count(status) over (partition by null) PercentageTotal
, count(case when Status = 'Passed' then 1 end) over (partition by Quality) * 100 / count(status) over (partition by Quality) PercentagePassed
, count(case when Status = 'Failed' then 1 end) over (partition by Quality) * 100 / count(status) over (partition by Quality) PercentageFailed
FROM test
您可能想要考虑切换到另一种支持小数的数据类型,但我在您的示例中保留了它。