sql行转列查询

SELECT
            recordtime,
            NewCollect=ISNULL(SUM(CASE Type2 WHEN 1 THEN newcnt END),0),
            CancelCollect=ISNULL(SUM(CASE Type2 WHEN 1 THEN cancelcnt END),0),
            NewKeyword=ISNULL(SUM(CASE Type2 WHEN 2 THEN newcnt end),0),
            CancelKeyword=ISNULL(SUM(CASE Type2 WHEN 2 THEN cancelcnt END),0),
            NewBlink=ISNULL(SUM(CASE Type2 WHEN 3 THEN newcnt end),0),
            CancelBlink=ISNULL(SUM(CASE Type2 WHEN 3 THEN cancelcnt END),0)
FROM B_Com

WHERE 1=1 {0}

GROUP BY recordtime

ORDER BY recordtime DESC

原文地址:https://www.cnblogs.com/whlhaikuotiankong/p/2455255.html