[英]SQL : Server dynamic PIVOT query
I am trying to make the below Pivot SQL query dynamic. 我正在尝试使以下Pivot SQL查询动态化。
List of the dynamic customer should be retrieved from below query. 应该从下面的查询中检索动态客户列表。
Customer List Query 客户清单查询
SELECT
Cust_Name
FROM dbo.Customer
INNER JOIN dbo.Service
ON dbo.Customer.Cust_ID = dbo.Service.Cust_ID
WHERE [Next_Service] BETWEEN '2017-09-01' AND '2015-09-10'
Pivot query 枢轴查询
Select *
from
(
select Customer.Cust_Name,Agreementlist.ProductQty,Store.Product_Name as Refill
from dbo.Agreement INNER JOIN dbo.Agreementlist ON dbo.Agreement.Agreement_ID = dbo.Agreementlist.Agreement_ID INNER JOIN dbo.Customer
ON dbo.Customer.Cust_ID = dbo.Agreement.Cust_ID INNER JOIN Store on Store.Pro_ID = dbo.Agreementlist.ProID where CatID='2' and Agreement.Status='Approved'
) x
pivot
(
sum(ProductQty)
for Cust_Name in ("list of customers resulted from the first query")
) p
I have used this SQL script to create dynamic Pivot 我已使用此SQL脚本创建动态数据透视表
CREATE PROCEDURE [dbo].[dynamicPivot]
(@tablename VARCHAR(250),
@pivotColumn VARCHAR(250),
@groupBy VARCHAR(8000),
@aggregateColumns VARCHAR(250),
@aggregation VARCHAR(250),
@execute INT = 0
)
AS
BEGIN
DECLARE @sql VARCHAR(8000), @pivotcols VARCHAR(8000)= '';
DECLARE @result TABLE(result VARCHAR(8000));
SET @sql = '
declare
@pivotcols varchar(8000) = '''';
with cte as (
select distinct '+@pivotColumn+', ''[''+cast('+@pivotColumn+' as varchar(250))+'']'' col
from '+@tablename+')
select @pivotcols = @pivotcols + col + '',''
from cte
order by '+@pivotColumn+';
set @pivotcols = left(@pivotcols, len(@pivotcols)-1);
select @pivotcols result';
INSERT INTO @result
EXEC (@sql);
SELECT @pivotcols = result
FROM @result;
SET @sql = '
SELECT *
FROM (
SELECT
'+@groupBy+CASE @groupBy
WHEN ''
THEN ''
ELSE ', '
END+@pivotColumn+', '+@aggregateColumns+'
FROM '+@tablename+'
) AS s
PIVOT
(
'+@aggregation+'
FOR '+@pivotColumn+' IN ('+@pivotcols+')
)AS p;';
IF @execute = 0
PRINT(@sql);
ELSE
EXEC (@sql);
END;
GO
You can add a cursor to loop over your customers and add them to a variable. 您可以添加游标来遍历客户并将其添加到变量中。 This here works.
这在这里有效。
declare @gruppe nvarchar(max)
declare @gruppeSql nvarchar(max)
declare @SQL nvarchar(max)
DECLARE myCustomers CURSOR FOR
SELECT Cust_Name FROM dbo.Customer INNER JOIN dbo.Service
ON dbo.Customer.Cust_ID = dbo.Service.Cust_ID
WHERE [Next_Service] BETWEEN '2017-09-01' AND '2015-09-10'
set @gruppeSql = ''
OPEN myCustomers
FETCH NEXT FROM myCustomers INTO @gruppe
IF (@@FETCH_STATUS>=0)
BEGIN
SET @gruppeSql = @gruppeSql +'[' +@gruppe+']'
FETCH NEXT FROM myCustomers INTO @gruppe
END
WHILE (@@FETCH_STATUS<>-1)
BEGIN
IF (@@FETCH_STATUS<>-2)
SET @gruppeSql = @gruppeSql + ',[' +@gruppe+']'
FETCH NEXT FROM myCustomers INTO @gruppe
END
CLOSE myCustomers
DEALLOCATE myCustomers
SET @gruppeSql = replace(@gruppesql,'''','')
--select @gruppeSql
SET @SQL = '
Select *
from
(
select Customer.Cust_Name,Agreementlist.ProductQty,Store.Product_Name as
Refill
from dbo.Agreement INNER JOIN dbo.Agreementlist ON
dbo.Agreement.Agreement_ID = dbo.Agreementlist.Agreement_ID INNER JOIN
dbo.Customer
ON dbo.Customer.Cust_ID = dbo.Agreement.Cust_ID INNER JOIN Store on
Store.Pro_ID = dbo.Agreementlist.ProID where CatID=''2'' and
Agreement.Status=''Approved''
) x
pivot
(
sum(ProductQty)
for Cust_Name in ('+@gruppesql+')
) p'
print @sql
exec(@sql)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.