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. unigui+fastReport实现web打印方案

    近日单位需要用到会议通知单的打印功能,故引出篇. unigui是delphi环境下快速开发web应用的优秀工具,不再赘述,下面直接记录使用搭配使用,基本逻辑就是: unigui实现数据录入和浏览的we ...

  2. 理解 Linux 的虚拟内存

    前言 前不久组内又有一次我比较期待的分享:”Linux 的虚拟内存”.是某天晚上加班时,我们讨论虚拟内存的概念时,leader 发现几位同事对虚拟内存认识不清后,特意给这位同学挑选的主题(笑). 我之 ...

  3. C实现动态进度条

    #include <iostream> #include <windows.h> void HideCursor() { CONSOLE_CURSOR_INFO cursor_ ...

  4. KVM虚拟机配置笔记

    KVM 全称是 Kernel-Based Virtual Machine.也就是说 KVM 是基于 Linux 内核实现的,KVM有一个内核模块叫 kvm.ko,只用于管理虚拟 CPU 和内存. 在 ...

  5. Python编程练习:使用 turtle 库完成玫瑰花的绘制

    绘制效果: 源代码: import turtle # 设置初始位置 turtle.penup() turtle.left(90) turtle.fd(200) turtle.pendown() tur ...

  6. 【sql注入教程】mysql注入直接getshell

    Mysql注入直接getshell的条件相对来说比较苛刻点把 1:要知道网站绝对路径,可以通过报错,phpinfo界面,404界面等一些方式知道 2:gpc没有开启,开启了单引号被转义了,语句就不能正 ...

  7. JavaScript原生实现观察者模式

    观察者模式又叫做发布订阅模式,它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态发生改变时就会通知所有观察着对象. 它是由两类对象组成,主题和观察者,主题负责发布事件 ...

  8. OAuth2.0认证详解

    目录 什么是OAuth协议 OAuth2.0是为了解决什么问题? OAuth2.0成员和授权基本流程 OAuth2.0成员 OAuth2.0基本流程 什么是OAuth协议 OAuth 协议为用户资源的 ...

  9. 《http权威指南》读书笔记4

    概述 最近对http很感兴趣,于是开始看<http权威指南>.别人都说这本书有点老了,而且内容太多.我个人觉得这本书写的太好了,非常长知识,让你知道关于http的很多概念,不仅告诉你怎么做 ...

  10. 移动端点击事件300ms延迟问题解决方案——fastclick.js

    移动端点击事件300ms延迟的问题由来已久,如下截图 下面截图来自原文:https://www.jianshu.com/p/6e2b68a93c88 网上关于300ms延迟问题的解决方法,大致分为 3 ...