Thursday, June 13, 2013

Piovt using sql query

SELECT * FROM (SELECT
convert(char(3), transaction_timestamp, 0) as transaction_timestamp1,transaction_serviceType FROM transactions_history 
where transaction_timestamp >='2013-01-01 00:00:00' and  transaction_timestamp<='2013-06-11 23:59:00'
and transaction_status in('OK'))
as PV
PIVOT
(
      count(transaction_timestamp1)
      --COUNT(transaction_serviceType)
      FOR transaction_timestamp1 IN ([JAN],[FEB],[MAR],[APR],[MAY],[JUN])
)

AS PVT 

and output would be as mention below:


transaction_serviceType JAN   FEB   MAR   APR   MAY   JUN
Sherforce               43    48    46    44    27    7
SherforcePlus           3     2     3     1     1     1
Shergroup Enforcement   2     6     2     13    16    16
ShergroupLegal          0     4     0     2     0     0