-2
A
回答
0
试试这个..
联盟所有枢轴..
declare @table table
(period nvarchar(10),num int,cost int,value int)
insert into @table
select 'Jan-16',1,100,100
union all select 'Feb-16',2,200,200
union all select 'Mar-16',3,300,300
union all select 'Apr-16',4,400,400
union all select 'May-16',5,500,500
union all select 'Jun-16',6,600,600
union all select 'Jul-16',7,700,700
union all select 'Aug-16',8,800,800
union all select 'Sep-16',9,900,900
union all select 'Oct-16',10,1000,1000
union all select 'Nov-16',11,1100,1100
union all select 'Dec-16',12,1200,1200
--select * from @table
select 'No of Order' as Period,[Jan-16], [Feb-16],[Mar-16],[Apr-16],[May-16],[Jun-16],[Jul-16],[Aug-16],[Sep-16],[Oct-16],[Nov-16],[Dec-16]
from
(
select period,num from @table) u
pivot (sum(num) for period in ([Jan-16] ,[Feb-16],[Mar-16],[Apr-16], [May-16], [Jun-16],[Jul-16],[Aug-16],[Sep-16],[Oct-16],[Nov-16], [Dec-16]))
as pvt
union all
select 'Cost Of Order' as Period,[Jan-16], [Feb-16],[Mar-16],[Apr-16],[May-16],[Jun-16],[Jul-16],[Aug-16],[Sep-16],[Oct-16],[Nov-16],[Dec-16]
from
(
select period,cost from @table) u
pivot (sum(Cost) for period in ([Jan-16] ,[Feb-16],[Mar-16],[Apr-16], [May-16], [Jun-16],[Jul-16],[Aug-16],[Sep-16],[Oct-16],[Nov-16], [Dec-16]))
as pvt
union all
select 'Value Of Order' as Period,[Jan-16], [Feb-16],[Mar-16],[Apr-16],[May-16],[Jun-16],[Jul-16],[Aug-16],[Sep-16],[Oct-16],[Nov-16],[Dec-16]
from
(
select period,value from @table) u
pivot (sum(value) for period in ([Jan-16] ,[Feb-16],[Mar-16],[Apr-16], [May-16], [Jun-16],[Jul-16],[Aug-16],[Sep-16],[Oct-16],[Nov-16], [Dec-16]))
as pvt
0
Select DaysToManufacture, AVG(StandardCost) As AverageCost
From Production.Product
Group by DaysToManufacture
Here is the result set.
DaysToManufacture AverageCost
0 5.0885
1 223.88
2 359.1082
4 949.4105
产品无三个DaysToManufacture定义。
以下代码显示了相同的结果,并进行了旋转,以便DaysToManufacture值成为列标题。即使结果为NULL,也会提供三天的列。
- 透视表一行五列
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost
FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;
下面是结果集。
Cost_Sorted_By_Production_Days 0 1 2 3 4
AverageCost 5.0885 223.88 359.1082 NULL 949.4105
4
0
我的例子:
DECLARE
@v_Columns VARCHAR(MAX),
@v_StartDate DATETIME = '09/15/2015',
@v_EndDate DATETIME = '12/15/2020',
@v_Query VARCHAR(MAX)
SELECT @v_Columns = COALESCE(@v_Columns,'[') + convert(varchar, Date, 101) + '],['
FROM
(SELECT DISTINCT Date FROM view_wc_sessions_info) th
WHERE
th.Date BETWEEN @v_StartDate AND @v_EndDate
SET @v_Columns = SUBSTRING(@v_Columns, 1, LEN(@v_Columns)-2)
SET @v_Query =
'SELECT
*
FROM
(
select [Total], Date, fk_student_rcid
from view_WC_SESSIONS_Info th
WHERE
th.Date BETWEEN ''' + CONVERT(VARCHAR(50), @v_StartDate, 101) + '''
AND ''' + CONVERT(VARCHAR(50), @v_EndDate, 101) + '''
) src
PIVOT
(
COUNT(src.[Total])
FOR src.Date IN (' + @v_Columns + ')
) AS pivotview'
EXEC(@v_Query)
相关问题
- 1. 数据透视行到列SQL 2008
- 2. 如何对SQL Server 2008数据透视表结果列和行进行排序?
- 3. 使用SQL列的数据在SQL中进行数据透视
- 4. 使用SQL Server 2008 R2的数据透视表
- 5. 使用SQL PIVOT行列... SQL Server 2008 R2
- 6. SQL Server 2008的列到行数据
- 7. 使用数据透视表减去列总计SQL Server 2008
- 8. SQL Server中的多列数据透视
- 9. 如何计算SQL Server 2008 R2中列的运行总数?
- 10. 透视使用SQL Server 2008 R2
- 11. 如何在SQL Server 2008的Text列中放置多行数据?
- 12. SQL Server 2008 R2中,转换列行和列的列
- 13. 在SQL Server 2008 R2中计数行
- 14. SQL数据透视表 - 多行多列
- 15. SQL数据透视行到列?
- 16. SQL数据透视表列行
- 17. 如何将SQL Server 2008 R2数据库还原到SQL Server 2008?
- 18. SQL Server 2008 R2:数据透视表的动态查询,其中有和子句
- 19. SQL Server数据透视表指定列
- 20. 作为列名数据透视表的SQL Server行值?
- 21. 关于SQL Server 2008 R2中列的元数据?
- 22. 如何在Sql Server 2008 R2中将列转换为行?
- 23. 的SQL Server 2008 R2:透视表
- 24. SQL Server 2008 R2的透视问题
- 25. SQL Server行要列数据
- 26. 使用sql server 2008 r2将行重新排列成列?
- 27. T-SQL数据透视表 - 总行数和动态列数
- 28. SQL Server 2008中获取一季度数据与数据透视
- 29. 如何在SQL Server 2000中执行动态数据透视
- 30. 动态SQL Server数据透视(UNPIVOT)列名到行值
我见过的链接只有答案,一个链接只问题是新的。请编辑你的问题来描述你的问题,描述你的模式,显示你的代码和样本数据 – HoneyBadger
其实我不能..我的公司不会允许我分享任何东西..这就是为什么我创建了这个rouf的东西来描述问题 –
哦,我大笑=)一张有要求的纸的照片,必须是SO中的第一张。 –