PIVOT查询以生成以下结果

时间:2019-02-13 10:17:00

标签: sql-server

Input Table
t1  sivaprakash 10  Jan       
t7  Mathi   50  Jan       
t8  Praveen 80  March     
t9  rarna   75  May       
t2  prasanna    70  Feb
t3  Pradeep 30  March     
t4  Mathon  35  April     
t5  Vijaykumar  40  May       
t6  Jeeva   50  June    

结果:

Month                 Jan Feb March April May June July

Number of Transactions 1   2    1    4     1    3    2

如何查询结果?

1 个答案:

答案 0 :(得分:1)

您还没有指定任何内容,但是如果您愿意,也可以尝试以下操作。

SELECT 'No Of Transaction' AS Month,* 
FROM   (SELECT 1 AS C1, 
               month 
        FROM   MySampleTable) t 
       PIVOT ( Count(c1) 
             FOR month IN ( [Jan],[Feb],[March],[April],[May],[June]))pvt 

Full Demo

+-------------------+-----+-----+-------+-------+-----+------+
| Month             | Jan | Feb | March | April | May | June |
+-------------------+-----+-----+-------+-------+-----+------+
| No Of Transaction | 2   | 1   | 2     | 1     | 2   | 1    |
+-------------------+-----+-----+-------+-------+-----+------+