sql server 行转列存储过程
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,,>
-- Description: <Description,,>
-- =============================================
create PROCEDURE [dbo].[P_GetPriceTableBuy]
@type nvarchar(),
@productId int=,
@ids nvarchar()='',
@keyword nvarchar()='',
@pageIndex int=,
@pageSize int=
AS
BEGIN
declare @id9 int ,@id10 int;
set @id9=
set @id10=
--采购二维码表页面展示
if @type='GetPriceTableBuy'
begin
declare @sqlcount nvarchar(max);
declare @psels nvarchar(max);
declare @pcols nvarchar(max);
set @psels=cast((select
',max(case t6.Name when '''+Name+''' then t4.ProductPropertyValueId else '''' end) as ['+Name+'ID],max(case t6.Name when '''+Name+''' then t4.Value else '''' end) as ['+Name+']'
from [ProductProperty] where ProductCatalogId= and ProductPropertyId not in(@id9,@id10) for xml path('')) as nvarchar(max))
set @pcols=cast((select ',['+name+'ID],['+name+']' from [ProductProperty] where ProductCatalogId= and ProductPropertyId not in(@id9,@id10) for xml path('')) as nvarchar(max))
select Name from [ProductProperty] where ProductCatalogId= and ProductPropertyId not in(@id9,@id10)
set @sqlcount=N' ;with cte as (
select ProductBrand_Name,Product_Name,ProductId
'+@pcols+'
from (select t2.ProductId,t2.Code,t2.Name as Product_Name,t1.BarCode,t2.ProductBrandId,
t5.Name as ProductBrand_Name
'+@psels+'
from [ProductSku] as t1
join [Product] t2 on t1.ProductId=t2.ProductId
join [ProductSkuProperty] t3 on t1.ProductSkuId=t3.ProductSkuId
join [ProductPropertyValue] t4 on t3.ProductPropertyValueId=t4.ProductPropertyValueId
join dbo.ProductBrand t5 on t2.ProductBrandId=t5.ProductBrandId
join dbo.ProductProperty t6 on t4.ProductPropertyId=t6.ProductPropertyId
where t1.[Status]= and t2.ProductCatalogId= and (t5.Name like ''%'+@keyword+'%'' or t2.Name like ''%'+@keyword+'%'')
group by t2.ProductId,t2.Name,t1.BarCode,t2.ProductBrandId,t5.Name,t2.Code) as a
group by ProductBrand_Name,Product_Name,ProductId'+@pcols+'
), c as
(
select ProductBrand_Name,Product_Name'+@pcols+',ProductId,row_number() over(order by ProductId) as num
from cte
)
select * from c where num between (@pageIndex-)*@pageSize+ and @pageIndex*@pageSize
union all
select top '''','''' '+@pcols+N',, count(*) over()from cte
';
--print(@sqlcount);
exec sp_executesql @sqlcount,N'@pageIndex int, @pageSize int',@pageIndex,@pageSize;
end
--采购二维码详情
if @type='UpdaPriceTableBuy'
begin
declare @sql nvarchar(max);
declare @sql2 nvarchar(max);
declare @sqlcou nvarchar(max)=N'';
declare @psels1 nvarchar(max);
declare @pcols1 nvarchar(max);
declare @where1 nvarchar(max);
declare @w1 nvarchar(max);
set @psels1=cast((select N',max(case t6.Name when '''+Name+N''' then t4.ProductPropertyValueId else 0 end) as ['+Name+'Id],max(case t6.Name when'''+Name+'''then t4.Value else '''' end) as ['+Name+']'
from [ProductProperty] where ProductCatalogId= and ProductPropertyId not in(@id9,@id10) for xml path('')) as nvarchar(max))
set @pcols1=cast((select N',['+name+N'Id],['+Name+N']'
from [ProductProperty] where ProductCatalogId= and ProductPropertyId not in(@id9,@id10) for xml path('')) as nvarchar(max))
set @where1=N'
set @w1=cast((select '' and a.[''+[name]+''Id]=''+cast(b.ProductPropertyValueId as nvarchar()) from [ProductProperty] a join ProductPropertyValue b on a.ProductPropertyId=b.ProductPropertyId
where ProductCatalogId= and a.ProductPropertyId not in(@id9,@id10)
and b.ProductPropertyValueId in ('+@ids+')
for xml path('''')) as nvarchar(max))
'
exec sp_executesql @where1,N'@id9 int,@id10 int,@w1 nvarchar(max) output',@id9,@id10,@w1 output;
set @sql= cast((select N',max(case when bValue='''+Value+N''' then ptb.BuyPrice else 0 end) as ['+value+N']' from ProductPropertyValue where ProductPropertyId=@id10 order by ProductPropertyValueId for xml path('')) as nvarchar(max));
set @sql2= cast((select N',max(case when bValue='''+Value+N''' then a2.ProductSkuId else 0 end) as ['+value+N'id]' from ProductPropertyValue where ProductPropertyId=@id10 order by ProductPropertyValueId for xml path('')) as nvarchar(max));
set @sqlcou+=N';with a1 as (
select a.ProductPropertyValueId as avid,a.Value as aValue,b.ProductPropertyValueId as bvid,b.Value as bValue
from ProductPropertyValue a cross join ProductPropertyValue b where a.ProductPropertyId =@id9 and b.ProductPropertyId=@id10
),a2 as (
select ProductSkuId,ProductId,Code,Product_Name,ProductBrandId,ProductBrand_Name
'+@pcols1+N'
,sphereid,sphere,lenticularid,lenticular
from (select t1.ProductSkuId,t2.ProductId,t2.Name as Product_Name,t1.BarCode,t2.Code,t2.ProductBrandId,
t5.Name as ProductBrand_Name
'+@psels1+N'
,MAX(case t6.Name when ''球镜'' then t4.ProductPropertyValueId else end)sphereid,
MAX(case t6.Name when ''球镜'' then t4.Value else '''' end)sphere,
MAX(case t6.Name when ''柱镜'' then t4.ProductPropertyValueId else end)lenticularid,
MAX(case t6.Name when ''柱镜'' then t4.Value else '''' end)lenticular
from [dbo].[ProductSku] as t1
join [Product] t2 on t1.ProductId=t2.ProductId
join [ProductSkuProperty] t3 on t1.ProductSkuId=t3.ProductSkuId
join [ProductPropertyValue] t4 on t3.ProductPropertyValueId=t4.ProductPropertyValueId
join dbo.ProductBrand t5 on t2.ProductBrandId=t5.ProductBrandId
join dbo.ProductProperty t6 on t4.ProductPropertyId=t6.ProductPropertyId
where t2.ProductCatalogId= and t1.ProductId=@pId
group by t2.ProductId,t2.Name,t1.BarCode,t2.ProductBrandId,t5.Name,t2.Code,t1.ProductSkuId) as a
where = '+@w1+N'
group by ProductSkuId,ProductId,Code,ProductBrandId,ProductBrand_Name,Product_Name
'+@pcols1+N'
,sphereid,sphere,lenticularid,lenticular
)
select a1.aValue'+@sql+N'
'+@sql2+N'
from a1
left join a2 on a1.avid=a2.sphereid and a1.bvid=a2.lenticularid
left join PriceTableBuy ptb on a2.ProductSkuId=ptb.ProductSkuId
group by a1.avid,a1.aValue
order by a1.avid';
exec sp_executesql @sqlcou,N'@id9 int,@id10 int,@pId int',@id9,@id10,@productId
end
END
结果图
最后拼接完成,并执行的SQL语句:
;with cte as (
select ProductBrand_Name,Product_Name,ProductId
,[材料ID],[材料],[折射率ID],[折射率],[曲率设计ID],[曲率设计],[阿贝数ID],[阿贝数],[加膜ID],[加膜],[类型ID],[类型],[定制ID],[定制],[年龄ID],[年龄]
from (select t2.ProductId,t2.Code,t2.Name as Product_Name,t1.BarCode,t2.ProductBrandId,
t5.Name as ProductBrand_Name
,max(case t6.Name when '材料' then t4.ProductPropertyValueId else '' end) as [材料ID],max(case t6.Name when '材料' then t4.Value else '' end) as [材料],max(case t6.Name when '折射率' then t4.ProductPropertyValueId else '' end) as [折射率ID],max(case t6.Name when '折射率' then t4.Value else '' end) as [折射率],max(case t6.Name when '曲率设计' then t4.ProductPropertyValueId else '' end) as [曲率设计ID],max(case t6.Name when '曲率设计' then t4.Value else '' end) as [曲率设计],max(case t6.Name when '阿贝数' then t4.ProductPropertyValueId else '' end) as [阿贝数ID],max(case t6.Name when '阿贝数' then t4.Value else '' end) as [阿贝数],max(case t6.Name when '加膜' then t4.ProductPropertyValueId else '' end) as [加膜ID],max(case t6.Name when '加膜' then t4.Value else '' end) as [加膜],max(case t6.Name when '类型' then t4.ProductPropertyValueId else '' end) as [类型ID],max(case t6.Name when '类型' then t4.Value else '' end) as [类型],max(case t6.Name when '定制' then t4.ProductPropertyValueId else '' end) as [定制ID],max(case t6.Name when '定制' then t4.Value else '' end) as [定制],max(case t6.Name when '年龄' then t4.ProductPropertyValueId else '' end) as [年龄ID],max(case t6.Name when '年龄' then t4.Value else '' end) as [年龄]
from [ProductSku] as t1
join [Product] t2 on t1.ProductId=t2.ProductId
join [ProductSkuProperty] t3 on t1.ProductSkuId=t3.ProductSkuId
join [ProductPropertyValue] t4 on t3.ProductPropertyValueId=t4.ProductPropertyValueId
join dbo.ProductBrand t5 on t2.ProductBrandId=t5.ProductBrandId
join dbo.ProductProperty t6 on t4.ProductPropertyId=t6.ProductPropertyId
where t1.[Status]= and t2.ProductCatalogId= and (t5.Name like '%%' or t2.Name like '%%')
group by t2.ProductId,t2.Name,t1.BarCode,t2.ProductBrandId,t5.Name,t2.Code) as a
group by ProductBrand_Name,Product_Name,ProductId,[材料ID],[材料],[折射率ID],[折射率],[曲率设计ID],[曲率设计],[阿贝数ID],[阿贝数],[加膜ID],[加膜],[类型ID],[类型],[定制ID],[定制],[年龄ID],[年龄]
), c as
(
select ProductBrand_Name,Product_Name,[材料ID],[材料],[折射率ID],[折射率],[曲率设计ID],[曲率设计],[阿贝数ID],[阿贝数],[加膜ID],[加膜],[类型ID],[类型],[定制ID],[定制],[年龄ID],[年龄],ProductId,row_number() over(order by ProductId) as num
from cte
)
select * from c where num between (@pageIndex-)*@pageSize+ and @pageIndex*@pageSize
union all
select top '','' ,[材料ID],[材料],[折射率ID],[折射率],[曲率设计ID],[曲率设计],[阿贝数ID],[阿贝数],[加膜ID],[加膜],[类型ID],[类型],[定制ID],[定制],[年龄ID],[年龄],, count(*) over()from cte
sql server 行转列存储过程的更多相关文章
- SQL Server 行转列重温
转载自http://www.cnblogs.com/kerrycode/ 行转列,列转行是我们在开发过程中经常碰到的问题.行转列一般通过CASE WHEN 语句来实现,也可以通过 SQL SERVER ...
- 【转载】SQL Server行转列,列转行
行转列,列转行是我们在开发过程中经常碰到的问题.行转列一般通过CASE WHEN 语句来实现,也可以通过 SQL SERVER 2005 新增的运算符PIVOT来实现.用传统的方法,比较好理解.层次清 ...
- 做图表统计你需要掌握SQL Server 行转列和列转行
说在前面 做一个数据统计和分析的项目,每天面对着各种数据,经过存储过程从源表计算汇总后需要写入中间结果表以提高数据使用效率,那么此时就需要用到行转列和列转行. 1.列转行 数据经过计算加工后会直接生成 ...
- Sql Server 行转列
--摘自百度 PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (… ...
- sql server 行转列(转载)
SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PI ...
- sql server 行转列 Pivot UnPivot
SQL Server中行列转换 Pivot UnPivot 本文转自:张志涛 原文地址: http://www.cnblogs.com/zhangzt/archive/2010/07/29/17878 ...
- SQL Server 行转列,列转行。多行转成一列
一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + va ...
- SQL Server 行转列,列转行
一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + va ...
- sql server 行转列解决方案
主要应用case语句来解决行转列的问题 行转列问题主要分为两类 1)简单的行转列问题: 示例表: id sid course result 1 2005001 语文 ...
随机推荐
- 了解AOP
Spring AOP的实现是基于JAVA的代理机制, 从JDK1.3开始就支持代理功能, 但是性能成为一个很大问题, 为了解决JDK代理性能问题, 出现了CGLIB代理机制.它可以生成字节码, 所以它 ...
- 河北大学python选修课00次作业
学习python认为挺好玩的一件事.看到很多关于python的东西在网上,看到有这个课,认为只是选修课,别人也可以选,自己想不能被别人落下,别人都会,我不会可不行. 而且认为python是一个很强大的 ...
- ETC的发展演变
ETC是不停车电子收费系统,ETC专用车道是给那些装了ETC车载器的车辆使用的,采用电子收费方式. ETC( Electronic Toll Collection ) 不停车收费系统是目前世界上最 ...
- 《JavaScript》高级程序设计第21章:Ajax和Comet,jsonp
一.创建XMLHttpRequest对象 二.XHR的用法 五.跨域资源共享 六.其他跨域技术七.安全七.安全 1. 图像Ping 2. JSONP(JSON with padding,填充式JSON ...
- 目标检测算法(2)SPP-net
本文是使用深度学习进行目标检测系列的第二篇,主要介绍SPP-net:Spatial Pyramid Pooling in Deep ConvolutionalNetworks for Visual R ...
- eclipse对于标签的配置不会出现自动提示的解决
解决办法:引入 mybatis-3-config.dtd 文件Window-preferences-搜索 xml-xml catalog在 User Specified Entries 目录下 add ...
- sublimeText3编辑器 + 入门教程 + 使用大全
sublimeText3编辑器 + 入门教程 + 使用大全 Ctrl+D选中光标所占的文本,继续操作则会选中下一个相同的的文本 ctrl+G:输入行号,可快速跳转该行 ctrl+p:输入冒号,在输入行 ...
- CSS揭秘—多重边框(二)
前言: 所有实例均来自<CSS揭秘>,该书以平时遇到的疑难杂症为引,提供解决方法,只能说秒极了,再一次刷新了我对CSS的认知 该书只提供了关键CSS代码,虽然有在线示例代码链接,但访问速度 ...
- Debian/Ubuntu清理硬盘空间的8个技巧
1. 删除残余的配置文件 通常Debian/Ubuntu删除软件包可以用两条命令 sudo apt-get remove <package-name> sudo apt-get purge ...
- 封装手风琴!使用jQuery!
//封装手风琴 /** * * * */ $.fn.accordion = function (colors, width) { var width=width||0; var colors= col ...