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 行转列存储过程的更多相关文章

  1. SQL Server 行转列重温

    转载自http://www.cnblogs.com/kerrycode/ 行转列,列转行是我们在开发过程中经常碰到的问题.行转列一般通过CASE WHEN 语句来实现,也可以通过 SQL SERVER ...

  2. 【转载】SQL Server行转列,列转行

    行转列,列转行是我们在开发过程中经常碰到的问题.行转列一般通过CASE WHEN 语句来实现,也可以通过 SQL SERVER 2005 新增的运算符PIVOT来实现.用传统的方法,比较好理解.层次清 ...

  3. 做图表统计你需要掌握SQL Server 行转列和列转行

    说在前面 做一个数据统计和分析的项目,每天面对着各种数据,经过存储过程从源表计算汇总后需要写入中间结果表以提高数据使用效率,那么此时就需要用到行转列和列转行. 1.列转行 数据经过计算加工后会直接生成 ...

  4. Sql Server 行转列

    --摘自百度 PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (… ...

  5. sql server 行转列(转载)

    SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PI ...

  6. sql server 行转列 Pivot UnPivot

    SQL Server中行列转换 Pivot UnPivot 本文转自:张志涛 原文地址: http://www.cnblogs.com/zhangzt/archive/2010/07/29/17878 ...

  7. SQL Server 行转列,列转行。多行转成一列

    一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + va ...

  8. SQL Server 行转列,列转行

    一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + va ...

  9. sql server 行转列解决方案

    主要应用case语句来解决行转列的问题 行转列问题主要分为两类 1)简单的行转列问题: 示例表: id  sid           course  result 1   2005001 语文     ...

随机推荐

  1. ubuntu自带截图工具--方便好用(转)

    一般用到的截图类型有三种:全屏.当前活动窗口.自定义区域,其中自定义区域截图是最灵活也是我们用的最多的方式.在ubuntu下可以通过其自带的截图工具轻松实现这三种功能. ubuntu自带的截图工具为s ...

  2. vue.js 2.0(1)

    1.点击一个按钮打开,关闭弹框 2.实现滚动监听,导航看顶置,实现某元素吸顶 路由

  3. 03 of learning python

    01 input输入的是str类型 如果输入的是数字的话,要记得强制转换一下! 02 isdigit() 这个方法是用来检测字符串是否全部由数字组成 str.isdigit() 如果字符串只包含数字则 ...

  4. C# WebSocket Fleck 调用非托管C++ DLL 实现通信(使用stringbuilder接收)

     [DllImport(@"XXX.dll", CallingConvention = CallingConvention.StdCall)]public static exter ...

  5. jsp页面时间戳转换为时间格式

    jstl中格式化时间戳   在jsp页面中使用jstl标签将long型的时间戳转换为格式化后的时间字符串 1.通过<jsp:useBean /> 导入java.util.Date类2.通过 ...

  6. feign调用spring clound eureka 注册中心服务

    @RestController public class TestService { private TestApi computeClient; private static final Strin ...

  7. [翻译][架构设计]The Clean Architecture

    原文地址:The Clean Architecture The Clean Architecture Over the last several years we've seen a whole ra ...

  8. 对js中闭包,作用域,原型的理解

    前几天,和朋友聊天,聊到一些js的基础的时候,有一种‘好像知道,好像又不不知道怎么讲的感觉’...于是捡起书,自己理一理,欢迎拍砖. 闭包 理解闭包首先要理解,js垃圾回收机制,也就是当一个函数被执行 ...

  9. 子弹短信光鲜的背后:网易云信首席架构师分享亿级IM平台的技术实践

    本文原文内容来自InfoQ的技术分享,本次有修订.勘误和加工,感谢原作者的分享. 1.前言 自从2018年8月20日子弹短信在锤子发布会露面之后(详见<老罗最新发布了“子弹短信”这款IM,主打熟 ...

  10. CSRF 攻击(跨域攻击)

    一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSR ...