如何在sql语句中使用多个pivot函数?

时间:2016-01-28 15:47:27

标签: sql

输入---

Order Status    Mode    Recipient
Confirmed       Mail    Customer
Confirmed        Fax    Customer
Confirmed      Mobile   Customer
Confirmed       Mail    Vendor
Confirmed        Fax    Vendor

输出---

  Event   Mail  Fax Mobile
Confirmed   3    3   3
Received    3    3   3
Dispatched  3    3   3

1 个答案:

答案 0 :(得分:0)

你的问题不明确。根据我的理解,您需要来自sql的数据透视表。

SELECT* FROM TableTest

SELECT *  FROM
(SELECT  OrderStatus, Mode FROM TableTest) AS P
PIVOT (Count(Mode) FOR Mode IN (Mail, Mobile, Fax )) AS pv

SELECT *  FROM
(SELECT  Recipient, Mode FROM TableTest ) AS P
PIVOT ( Count(Mode) FOR Mode IN (Mail, Mobile, Fax )) AS pv

enter image description here