查询以从SQL Server 2008中具有重复行的表中检索列值作为行

时间:2016-12-07 16:57:04

标签: sql-server

我有一张表格,其中包含以下格式的记录:

OrderID | CategoryID | Percentage
-----------------------------------------
 Order1       1           50
 Order1       2          100
 Order1       3           70
 Order2       1           85
 Order2       2           75
 Order2       3          100
 Order3       1           50
 Order3       2           60
 Order3       3           80

每个订单都有多个类别,每个类别都有一个与该订单相对应的百分比。

我想以下列格式显示它:

OrderID | Category1_%  |  Category2_%  |  Category3_%
-------------------------------------------------------
Order1        50            100             70
Order2        85             75            100
Order3        50             60             80

如何在SQL Server 2008中编写查询以检索每个(不同)订单的每个类别的百分比?请帮忙。

修改     订单表

ID      |  StartDate  
--------------------
Order1     1/1/2016         
Order2     10/1/2015           
Order3     10/5/2016      

我执行的查询是:

Declare @SQL varchar(max)

Declare @startDate datetime

set @startDate='1/1/2015'

Declare @endDate datetime

set @endDate= '10/1/2016'

Select  @SQL = Stuff((Select Distinct ',' + QuoteName('Category'+cast(CategoryID as varchar(25))+'_%') From OrdersData Order By 1 For XML Path('')),1,1,'')

Select  @SQL = 'Select [OrderID],' + @SQL +'
From (Select OrderID,Percentage,Item=''Category''+cast(CategoryID as varchar(25))+''_%'' from OrdersData join Orders on Orders.ID =OrdersData.OrderID 
WHERE Orders.StartDate >= '+ ***convert(nvarchar(25), @startDate, 121)*** + ' and GCWR.StartDate <= '+  ***convert(nvarchar(25), @endDate, 121)*** +') A
Pivot (max(Percentage) For Item in (' + @SQL + ') ) p'

Exec(@SQL);

当我执行时,我会在&#39; 00&#39;附近收到错误&#39;错误的语法。&#39;如何调用上述查询中的日期时间值来显示结果?请告诉我。

2 个答案:

答案 0 :(得分:1)

以防你需要动态

Declare @SQL varchar(max) 
Select  @SQL = Stuff((Select Distinct ',' + QuoteName('Category'+cast(CategoryID as varchar(25))+'_%') From YourTable Order By 1 For XML Path('')),1,1,'') 
Select  @SQL = 'Select [OrderID],' + @SQL +'
                From (Select OrderID,Percentage,Item=''Category''+cast(CategoryID as varchar(25))+''_%'' from YourTable) A
                Pivot (max(Percentage) For Item in (' + @SQL + ') ) p'
Exec(@SQL);

返回

enter image description here

  

修改 - 使用StartDate

更新
Declare @Date1 datetime = '2015-01-01'
Declare @Date2 datetime = '2016-10-01'

Declare @SQL varchar(max) 
Select  @SQL = Stuff((Select Distinct ',' + QuoteName('Category'+cast(CategoryID as varchar(25))+'_%') From YourTable Where cast(StartDate as date) between @Date1 and @Date2  Order By 1 For XML Path('')),1,1,'') 
Select  @SQL = 'Select [OrderID],' + @SQL +'
                From (Select OrderID,Percentage,Item=''Category''+cast(CategoryID as varchar(25))+''_%'' from YourTable Where cast(StartDate as date) between '''+convert(varchar(10),@Date1,120)+''' and '''+convert(varchar(10),@Date2,120)+''') A
                Pivot (max(Percentage) For Item in (' + @SQL + ') ) p'
Exec(@SQL);

返回

enter image description here

答案 1 :(得分:0)

尝试一下:

Select OrderID, [1] as [Category1_%], [2] as [Category2_%],[3] as [Category3_%] 
From [TableName]
PIVOT (MAX([Percentage]) FOR [CategoryID] IN ([1],[2],[3])) myPivot 

这假设您知道categoryID字段中的所有可能值,并且每个CategoryID和OrderID组合都有一个百分比值。