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. 转 Refresh Excel Pivot Tables Automatically Using SSIS Script Task

    Refresh Excel Pivot Tables Automatically Using SSIS Script Task https://www.mssqltips.com/sqlservert ...

  2. MapReduce实例学习

    https://blog.csdn.net/m0_37739193/article/details/77676859

  3. git 团队开发常用操作流程(适用于 gogs、gitlab、github)

    git 团队开发常用操作流程(适用于 gogs.gitlab.github) NO1 项目构建者 (1)在远程仓库创建仓库 (2)将伙伴添加到仓库合作者中(无先后要求) (2)cd 到项目将要存放项目 ...

  4. 我所理解Java集合框架的部分的使用(Collection和Map)

    所谓集合,就是和数组类似——一组数据.java中提供了一些处理集合数据的类和接口,以供我们使用. 由于数组的长度固定,处理不定数量的数据比较麻烦,于是就有了集合. 以下是java集合框架(短虚线表示接 ...

  5. apache与tomcat负载集群集成方法配置

    apache与tomcat负载集群集成方法有3种jk.jk_proxy.http_proxy apache:httpd-2.2.17-win32-x86-no_ssl.msi tomcat:apach ...

  6. 包建强的培训课程(3):App竞品技术分析

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  7. YxCMS 1.4.7 最新版漏洞分析

    i春秋作家:F0rmat 原文来自:YxCMS 1.4.7 最新版漏洞分析 0x01前言 很感谢关注我专辑的表哥,我会坚持写下去的,最近会慢一点,一月四篇是正常的. 在先知看到的,大部分都是后台漏洞, ...

  8. 剑指offer【04】- 重建二叉树(java)

    题目:重建二叉树 考点:树 题目描述:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6, ...

  9. constructor属性解析

    JavaScript中constructor属性一直不是很清楚,今日终于弄清了其中缘由,下面举例说明. 首先是一个典型的JavaScript实例声明: function Person(){ this. ...

  10. list源码4(参考STL源码--侯捷):transfer、splice、merge、reverse、sort

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...