且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL Server中的数据透视图中的聚合函数上使用case语句

更新时间:2023-02-07 09:57:41

,也许您可​​以张贴到目前为止已编写的代码.它将帮助我们提供最后一步.
Perhaps you could post the code you have written so far. It would help us provide the last step.




您不能在枢轴中将case语句与聚合函数一起使用.

请参考以下URL:-http://technet.microsoft.com/zh-cn/library/ms177410.aspx

如果要使用case语句,则可以在使用了ivot关键字的实际select语句中使用.检查以下语法:-
SELECT``Sun''=
如果[Sun]> 0则为"true",否则为"false"结束,
``星期一''=
[Mon]> 0时为"true",否则为"false"的情况,
``Tue''=
[Tue]> 0时为"true",否则为"false"结束的情况

来自
(选择
天数,费用
来自
产品
)P
PIVOT(
总和(费用)

FOR
天数
(
[Sun],
[星期一],
[Tue]
))AS PVT

这样,您可以在查询中使用case语句. :)

谢谢

Mehul Thakkar
Hi,

you can not use case statement along with aggregate function in pivot.

refer the following URL :- http://technet.microsoft.com/en-us/library/ms177410.aspx

If you want to use case statement then you can use in actual select statement in which you have used pivot keyword. check the following syntax:-
SELECT ''Sun''=
case when [Sun]>0 then ''true'' else ''false'' end,
''Mon'' =
case when [Mon]>0 then ''true'' else ''false'' end,
''Tue'' =
case when [Tue]>0 then ''true'' else ''false'' end

FROM
(Select
Days, Cost
From
Product
)P
PIVOT(
Sum(Cost)

FOR
Days IN
(
[Sun],
[Mon],
[Tue]
)) AS PVT

This way you can use case statement in your query. :)

Thanks

Mehul Thakkar