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 语文 ...
随机推荐
- 命令行方式登录PostgreSQL
目录: 一.在默认配置条件下,本机访问PostgreSQL 二.创建新用户来访问PostgreSQL 三.最简单的做法 四.开启远程访问 一.在默认配置条件下,本机访问PostgreSQL 切换到Li ...
- JVM之垃圾收集器与内存分配回收策略(二)
上一篇JVM垃圾收集器与内存分配策略(一),下面是jdk1.7版本的垃圾收集器之间的关系,其中连线两端的两种垃圾收集器可以进行搭配使用,下面来总结一下这些收集器的一些特点以及关系. 一.Serial收 ...
- win7中调试Hotspot
预置: 1.Visual Studio 2010 2.CYGWIN(设置到path中) 3.jdk(设置到path中) 4.Framework4(系统如果有就不需要安装) 5.win7 sdk 7.1 ...
- Linux(Ubuntu18.04)安装Chrome浏览器
一分钟安装教程! 1.将下载源加入到系统的源列表(添加依赖) sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt ...
- Git使用详细教程(7):.gitignore使用详解
Git提供了一个.gitignore文件,帮助我们忽略掉一些不想或者不能提交到版本控制器中的文件.这个文件的使用时必须要掌握的. *.a # 忽略所有目录下的.a结尾的文件 !lib.a # 但lib ...
- 小程序开发基础-scroll-view 可滚动视图区域
小编 / 达叔小生 小程序开发基础-scroll-view 可滚动视图区域 这里只展示纵向滚动,横向同理就不用说明了,可自己尝试,横向滚动属性为scroll-x,把纵向滚动改为横向滚动即可. scro ...
- 单元测试mock当前时间
在实际项目中很多地方用到DateTime.Now,这个时间是时时变化的.如果要进行单元测试对比预期结果时,这个时间无法预测,可以添加如下两个时间类 namespace Common.Helper { ...
- 线性插值法的原理和python代码实现
假设我们已知坐标 (x0, y0) 与 (x1, y1),要得到 [x0, x1] 区间内某一位置 x 在直线上的值.根据图中所示,我们得到 由于 x 值已知,所以可以从公式得到 y 的值 已知 y ...
- C# 在窗体上可拖动控件
最近做了一个标签打印配置功能,需要根据客户需求自定义标签格式.显示内容,这时就用到了后台生成控件,并且其控件可在窗口中进行拖动,这里仅为记录一下实现过程,方便以后使用. 结果图: 源码: using ...
- 2.matplotlib画散点图
2.1.身高和体重实例 import matplotlib.pyplot as plt height = [161,162,163,164,165] weight = [50,60,70,80,90] ...