1. 程式人生 > >數據統計--union all 執行多條sql

數據統計--union all 執行多條sql

name HERE spa nbsp 執行 通過 code ble clas

需求--統計hive某張表type字段不同取值的數據量

我們已知某張表的type的取值是1,2,3,4,5,想要統計不同type的數據量,並清晰的展現出來。可以通過union all 的方式,sql如下:

SELECT "1",COUNT(*) FROM table_name WHERE type = "1"
UNION ALL
SELECT "2",COUNT(*) FROM table_name WHERE type = "2"
UNION ALL
SELECT "3",COUNT(*) FROM table_name WHERE type = "3"
UNION ALL
SELECT 
"4",COUNT(*) FROM table_name WHERE type = "4" UNION ALL SELECT "5",COUNT(*) FROM table_name WHERE type = "5"

得到的結果如下:

type count(*)
1 324
2 123
3 345
4 156
5 343

數據統計--union all 執行多條sql