sql列转行

1.需要实现一个单行的统计报表
思路先用一个union查出单列,然后再把单列转成单行
2.实现
SELECT
MAX(CASE WHEN type = 1 THEN num ELSE 0 END) AS 'GeneralCnt',
MAX(CASE WHEN type = 2 THEN num ELSE 0 END) AS 'HistoryCnt'
from (
select '1' as 'type',count(*) as 'num'
from General
union all
select '2' ,count(*)
from History
) tmp
原文地址:https://www.cnblogs.com/kenny999/p/3713035.html