基本语法 select * from Mould pivot ( count(ID)for ProductTypeCode in ( [FC], [RCU], [RCD] )) as PVT; with P as ( select * from Mould ) select * from P pivot ( count(ID)for ProductTypeCode in ( [FC], [RCU], [RCD] )) as PVT; 一.sql行转列PIVOT: 二.sql列转行unPIVOT:
SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (-) )AS P 完整语法: table_source PIVOT( 聚合函数(value_column) FOR pivot_column IN(<column_list>) ) UNPIVOT用于将列明转为列值(即列转行),在SQL Server 2
有同学问了个比较典型行列转换的问题,想想,解答如下:数据库有一张表: 是个公司采购表,想转化成如下报表,显示每个公司各采购了些什么产品: 哪些公司采购哪些产品是不确定的,所以报表的列有哪几项是不确定的,我想了很久也没什么好方法,请问有办法吗? 解题思维步骤: 先找到未行列转换的数据,分组查看数据试试:select CompanyName,ProductName,COUNT(*)as num from dbo.Orders group by ProductName,CompanyName orde
SQLSERVER 2005 以后提供了新的方式进行行列转换,下面是一个实例供参考: if object_id('tb') is not null drop table tbTest go ),季度 ),销售额 int) ) ) ) ) ) ) ) ) ) ) ) ) go select * from tbTest go --sqlserver 2000 或者其它数据库 select 产品 , end) as 一季度, end) as 二季度, end) as 三季度, end) as 四季度
行列转换:SELECT max(case type when 0 then jine else 0 end) a,max(case type when 1 then jine else 0 end) b,max(case type when 2 then jine else 0 end) cFROM Table WHERE tuanid='402881e44365c0dc014366a4b47e0114'group by tuanid
行列转换,将列数据转换为字符串输出 ) SET @center_JZHW = ( SELECT DISTINCT STUFF( ( SELECT ',' + ce_code FROM ap_center WITH ( NOLOCK ) WHERE CE_PROVINCE = '浙江省' ORDER BY ce_code , , '') AS jzhw FROM ap_center t) SELECT @center_JZHW Function写法 ) SET @ret = '' SELECT @