gpt4 book ai didi

sql - 动态创建列sql

转载 作者:行者123 更新时间:2023-12-01 19:48:16 25 4
gpt4 key购买 nike

我有一张客户表

Customer ID        Name         
1 John
2 Lewis
3 Mary

我还有另一个表 CustomerRewards

 TypeID           Description
1 Bronze
2 Silver
3 Gold
4 Platinum
5 AnotherOne

决赛 table

 RewardID          TypeID          CustomerID
1 1 1
2 1 1
3 2 1
4 2 2

customerTypes 表是动态的,可以添加和删除其中许多类型。基本上我想要的是动态生成的列和每个列的计数,类似于

CustomerName        Bronze        Silver     Gold      Platinum     AnotherOne    total
John 2 1 0 0 0 3
Lewis 0 1 0 0 0 1
Grand TOTAL 2 2 0 0 0 4

问题就像我说的那样,类型是动态的,客户是动态的,所以我需要根据系统中的类型使列是动态的

我已经标记了 c#,因为我在 DataGridView 中需要它

提前致谢

最佳答案

您将需要为此使用PIVOT 函数。如果您有已知的列数,则可以对值进行硬编码:

select name, [Bronze], [Silver], [Gold], [Platinum], [AnotherOne]
from
(
select c.name,
cr.description,
r.typeid
from customers c
left join rewards r
on c.id = r.customerid
left join customerrewards cr
on r.typeid = cr.typeid
) x
pivot
(
count(typeid)
for description in ([Bronze], [Silver], [Gold], [Platinum], [AnotherOne])
) p;

参见SQL Fiddle with Demo .

现在,如果您的列数未知,那么您可以使用动态 SQL 来PIVOT:

DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(description)
from customerrewards
group by description, typeid
order by typeid
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')

set @query = 'SELECT name,' + @cols + ' from
(
select c.name,
cr.description,
r.typeid
from customers c
left join rewards r
on c.id = r.customerid
left join customerrewards cr
on r.typeid = cr.typeid
) x
pivot
(
count(typeid)
for description in (' + @cols + ')
) p '

execute(@query)

参见SQL Fiddle With Demo

如果您需要包含 Total 列,则可以使用 ROLLUP ( Static Version Demo ):

select name, sum([Bronze]) Bronze, sum([Silver]) Silver, 
sum([Gold]) Gold, sum([Platinum]) Platinum, sum([AnotherOne]) AnotherOne
from
(
select name, [Bronze], [Silver], [Gold], [Platinum], [AnotherOne]
from
(
select c.name,
cr.description,
r.typeid
from customers c
left join rewards r
on c.id = r.customerid
left join customerrewards cr
on r.typeid = cr.typeid
) x
pivot
(
count(typeid)
for description in ([Bronze], [Silver], [Gold], [Platinum], [AnotherOne])
) p
) x
group by name with rollup

动态版本(Demo):

DECLARE @cols AS NVARCHAR(MAX),
@colsRollup AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(description)
from customerrewards
group by description, typeid
order by typeid
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')

select @colsRollup
= STUFF((SELECT ', Sum(' + QUOTENAME(description) + ') as '+ QUOTENAME(description)
from customerrewards
group by description, typeid
order by typeid
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')


set @query
= 'SELECT name, '+ @colsRollup + '
FROM
(
SELECT name,' + @cols + ' from
(
select c.name,
cr.description,
r.typeid
from customers c
left join rewards r
on c.id = r.customerid
left join customerrewards cr
on r.typeid = cr.typeid
) x
pivot
(
count(typeid)
for description in (' + @cols + ')
) p
) x1
GROUP BY name with ROLLUP'

execute(@query)

关于sql - 动态创建列sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12643117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com