简体   繁体   中英

Categorize using spark sql

I have a table like following 在此输入图像描述

I want to summeraize it in to following table

在此输入图像描述

How to do it using at least sql?

Edit class3 count should be 2. That's a typo

This can be done in SQL as below

SELECT Org, SUM(CASE WHEN class='class1' THEN 1 ELSE O END) As Class1Count,
       SUM(CASE WHEN class='class2' THEN 1 ELSE O END) As Class2Count,
       SUM(CASE WHEN class='class3' THEN 1 ELSE O END) As Class3Count,
       SUM(CASE WHEN class='class4' THEN 1 ELSE O END) As Class4Count
FROM tableName
GROUP By Org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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