Sql 根据salesperson将行更改为列
我有一张桌子,上面放着一些销售人员销售的产品。销售后,不同的销售人员会根据其职位以不同的百分比获得相同产品的特许权使用费。相同产品但不同销售人员的所有行显示在不同的行中,但如果产品和transactionID相同,我希望它显示在同一行中。但销售人员姓名和版税应按以下示例显示。我也尝试了动态轴心和动态交叉表,但没能解决它。可能是我没能正确使用它。谁能帮我一下吗Sql 根据salesperson将行更改为列,sql,sql-server,Sql,Sql Server,我有一张桌子,上面放着一些销售人员销售的产品。销售后,不同的销售人员会根据其职位以不同的百分比获得相同产品的特许权使用费。相同产品但不同销售人员的所有行显示在不同的行中,但如果产品和transactionID相同,我希望它显示在同一行中。但销售人员姓名和版税应按以下示例显示。我也尝试了动态轴心和动态交叉表,但没能解决它。可能是我没能正确使用它。谁能帮我一下吗 TranID Product qty UntPrice TotPrice JOHN LUCY 10011 Lapt
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
TranID Product Qty UntPrice TotPrice SalesPrsn Royalty% Royalty$ SalesPrsn Royalty% Royalty$
10011 Laptop 2 75 150 JOHN 10% 15 LUCY 5% 7.5
10012 Camara 3 15 45 JOHN 10% 4.5 LUCY 5% 2.25
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
我尝试过动态轴心:(我不希望这样的结果)
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
TranID Product Qty UntPrice TotPrice SalesPrsn Royalty% Royalty$ SalesPrsn Royalty% Royalty$
10011 Laptop 2 75 150 JOHN 10% 15 LUCY 5% 7.5
10012 Camara 3 15 45 JOHN 10% 4.5 LUCY 5% 2.25
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
原始数据:
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
TranID Product Qty UntPrice TotPrice SalesPrsn Royalty% Royalty$
10011 Laptop 2 75 150 JOHN 10% 15
10012 Camara 3 15 45 JOHN 10% 4.5
10011 Laptop 2 75 150 LUCY 5% 7.5
10012 Camara 3 15 45 LUCY 5% 2.25
TranID Product Qty UntPrice TotPrice SalesPrsn Royalty% Royalty$ SalesPrsn Royalty% Royalty$
10011 Laptop 2 75 150 JOHN 10% 15 LUCY 5% 7.5
10012 Camara 3 15 45 JOHN 10% 4.5 LUCY 5% 2.25
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
预期结果数据:
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
TranID Product Qty UntPrice TotPrice SalesPrsn Royalty% Royalty$ SalesPrsn Royalty% Royalty$
10011 Laptop 2 75 150 JOHN 10% 15 LUCY 5% 7.5
10012 Camara 3 15 45 JOHN 10% 4.5 LUCY 5% 2.25
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
使用动态轴获得:(我不希望这样的结果)
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
TranID Product Qty UntPrice TotPrice SalesPrsn Royalty% Royalty$ SalesPrsn Royalty% Royalty$
10011 Laptop 2 75 150 JOHN 10% 15 LUCY 5% 7.5
10012 Camara 3 15 45 JOHN 10% 4.5 LUCY 5% 2.25
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
如果有人能帮助我,我们将不胜感激。结果中的列名必须是唯一的,这样您就不能为不同的销售人员重复salesPrsn etc列。下面的脚本将为您提供预期的结果,但列名的前缀是人名。您可以将其命名为任何您想要的名称,例如SalesPrsn1、SalesPrn2等
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
TranID Product Qty UntPrice TotPrice SalesPrsn Royalty% Royalty$ SalesPrsn Royalty% Royalty$
10011 Laptop 2 75 150 JOHN 10% 15 LUCY 5% 7.5
10012 Camara 3 15 45 JOHN 10% 4.5 LUCY 5% 2.25
TranID Product qty UntPrice TotPrice JOHN LUCY
10011 Laptop 2 75 150 15 7.5
10012 Camara 3 15 45 4.5 2.25
Create table #Royalty
(
TranID int,
Product char(10),
Qty int,
UntPrice money,
TotPrice money,
SalesPrsn char(10),
[Royalty%] int,
[royalty$] money
)
insert into #Royalty values (10011,'Laptop', 2, 75,150,'JOHN',10,15)
insert into #Royalty values (10012,'Camara', 3, 15,45,'JOHN',10,4.5)
insert into #Royalty values (10011,'Laptop', 2, 75,150,'LUCY',5,7.5)
insert into #Royalty values (10012,'Camara', 3, 15,45,'LUCY',5,2.25)
select * from #Royalty
DECLARE @SalesPerson TABLE (SalesPrsn char(10), Rid int identity not null);
INSERT @SalesPerson(SalesPrsn)
SELECT DISTINCT SalesPrsn FROM #Royalty;
-- SELECT * FROM @SalesPerson;
DECLARE @SQL nvarchar(max);
DECLARE @RID int;
DECLARE @Prsn char(10);
SET @SQL=N'
SELECT TranID, MAX(Product) AS Product, MAX(Qty) AS Qty, MAX(UntPrice) AS UntPrice, MAX(TotPrice) AS TotPrice'
SELECT @RId=MIN(RID) FROM @SalesPerson;
WHILE @RID IS NOT NULL
BEGIN
SELECT @Prsn=SalesPrsn FROM @SalesPerson WHERE Rid=@RID;
SET @SQL=@SQL+N',
MAX(CASE WHEN SalesPrsn='''+@Prsn+''' THEN SalesPrsn ELSE NULL END) AS ['+RTRIM(@Prsn)+'_SalesPrsn],
MAX(CASE WHEN SalesPrsn='''+@Prsn+''' THEN [Royalty%] ELSE NULL END) AS ['+RTRIM(@Prsn)+'_Royalty%],
MAX(CASE WHEN SalesPrsn='''+@Prsn+''' THEN [Royalty$] ELSE NULL END) AS ['+RTRIM(@Prsn)+'_Royalty$]'
SELECT @RId=MIN(RID) FROM @SalesPerson WHERE Rid>@RID;
END
SET @SQL=@SQL+'
FROM #Royalty
GROUP BY TranID;'
EXEC sp_executesql @SQL;
如果这个查询是针对类似SSRS报告的报告,那么在那里做起来会容易得多谢谢你的回复,但它不是针对SSRS的。谢谢你Peter,让我试试这个代码。非常感谢你的帮助