创建表: IF OBJECT_ID('T040_PRODUCT_SALES') IS NOT NULL DROP TABLE T040_PRODUCT_SALES create table T040_PRODUCT_SALES ( ID INT IDENTITY(1,1), ProductName VARCHAR(20), SaleMonth INT, SalesCount INT ) 插入数据并排序: INSERT INTO T040_PRODUCT_SALES VALUES ('Bicycl
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
--摘自百度 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 2000可以用UNION来实现 完整语法: ta
一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + value FROM A WHERE name = Test.name FOR XML PATH('') ), , , '') ) FROM A AS Test GROUP BY name; PS:STUFF语句就是为了去掉第一个[逗号] 附STUFF用法:(从原字符的第二个开始共三个字符替换为后面的字符) ,
一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + value FROM A WHERE name = Test.name FOR XML PATH('') ), 1, 1, '') ) FROM A AS Test GROUP BY name; PS:STUFF语句就是为了去掉第一个[逗号] 附STUFF用法:(从原字符的第二个开始共三个字符替换为后面的字符)
1.简单案例 create database Hang go use Hang create table Students ( Name varchar(50), Kemu varchar(50), sor int ) insert into Students values('张三','语文',8888) insert into Students values('张三','语文',85) insert into Students values('张三','数学',75) insert into
1: 行转列 子查询,获取一定数据集结果 SELECT objid,action,count(1) AS [count] FROM T_MyAttention WHERE objid IN(SELECT TOP 10 objid FROM T_MyAttention tma GROUP BY objid ORDER BY count(1) DESC)GROUP BY objid,action 下面用 行转列语法获取 最终结果 select *from (SELECT objid,action,
select * from ( select mvqr.VoteQuestionId,mvqr.AnswerSolution from JY_MemberVoteQuestionRef as mvqr where mvqr.MemberVoteId in (select MemberVoteId from JY_MemberVote where VoteId='166') ) as t pivot (max(t.AnswerSolution) for t.VoteQu
1.多行转成一行(并以','分开) 表数据如下图: 查询结果如下图: SQL查询脚本: SELECT addPer, house_code = (STUFF((SELECT ',' + house_code FROM my_demo WHERE addPer = T.addPer FOR XML PATH('')), 1, 1, '')) FROM my_demo AS T GROUP BY addPer 2.一列转为一行 SELECT STUFF((SELECT ',' + CAST(id A
if object_id('[P_GetPriceTableBuy]','P') is not null drop procedure P_GetPriceTableBuy SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,>
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