我在报表生成器中有一个奇怪的行为。 在尝试使用报表生成器之前,我正在使用现有数据集并在SQL studio中测试我自己的代码。我迷路了,因为我不明白为什么以下不起作用:
SELECT
v_Collection_Alias.Name as CollectionName,
v_Package_Alias.Name as SoftwareName,
'Package' as ApplicationType,
NumberSuccessTable='NumberSuccessTable', sum(case when stat.LastState in (-1,13) then 1 else 0 end) as NumberSuccess,
NumberInProgressTable='NumberInProgress', sum(case when stat.LastState in (8,9) then 1 else 0 end) as NumberInProgress,
NumberUnknownTable='NumberUnknown', sum(case when stat.LastState in (0) then 1 else 0 end) as Unknown,
NumberErrorTable='NumberError', sum(case when stat.LastState in (11) then 1 else 0 end) as NumberError,
NumberOtherTable='NumberOther', sum(case when stat.LastState in (10) then 1 else 0 end) as NumberOther,
'' as LastModifiedby,
'' as Version,
v_Advertisement_Alias.CollectionID as CollectionID,
v_Advertisement_Alias.AdvertisementID as DeploymentID,
'' as CI_ID,
'' as DeploymentTime,
v_Advertisement_Alias.PresentTime as ModificationTime,
'' as AssignmentID
FROM fn_rbac_Advertisement(@UserSIDs) v_Advertisement_Alias
JOIN fn_rbac_ClientAdvertisementStatus(@UserSIDs) stat on v_Advertisement_Alias.AdvertisementID = stat.AdvertisementID
INNER JOIN fn_rbac_Package2(@UserSIDs) v_Package_Alias ON v_Advertisement_Alias.PackageID = v_Package_Alias.PackageID
INNER JOIN fn_rbac_Collection(@UserSIDs) v_Collection_Alias ON v_Advertisement_Alias.CollectionID = v_Collection_Alias.CollectionID
此报告生成器提示我输入错误,因为fn_rbac_Advertisement.Name
需要group by子句。以下内容适用于原始报告:
SELECT
v_Collection_Alias.Name as CollectionName,
v_Package_Alias.Name as SoftwareName,
'' as ApplicationType,
'' as NumberSuccess,
'' as NumberInProgress,
'' as NumberUnknown,
'' as NumberErrors,
'' as NumberOther,
'' as LastModifiedby,
'' as Version,
v_Advertisement_Alias.CollectionID as CollectionID,
v_Advertisement_Alias.AdvertisementID as DeploymentID,
'' as CI_ID,
'' as DeploymentTime,
v_Advertisement_Alias.PresentTime as ModificationTime,
'' as AssignmentID,
'' as ApplicationType
FROM fn_rbac_Advertisement(@UserSIDs) v_Advertisement_Alias
INNER JOIN fn_rbac_Package2(@UserSIDs) v_Package_Alias ON v_Advertisement_Alias.PackageID = v_Package_Alias.PackageID
INNER JOIN fn_rbac_Collection(@UserSIDs) v_Collection_Alias ON v_Advertisement_Alias.CollectionID = v_Collection_Alias.CollectionID
以下内容返回我在SQL Studio中想要的内容:
Select
NumberSuccessTable='NumberSuccessTable', sum(case when stat.LastState in (-1,13) then 1 else 0 end),
NumberInProgressTable='NumberInProgress', sum(case when stat.LastState in (8,9) then 1 else 0 end),
NumberUnknownTable='NumberUnknown', sum(case when stat.LastState in (0) then 1 else 0 end) AS NumberU,
NumberErrorTable='NumberError', sum(case when stat.LastState in (11) then 1 else 0 end) AS NumberError,
NumberOtherTable='NumberOther', sum(case when stat.LastState in (10) then 1 else 0 end) as NumberOther
From v_ClientAdvertisementStatus stat
感谢您的帮助! :)
答案 0 :(得分:1)
使用聚合函数(例如SUM)时,必须将GROUP BY子句应用于所有未聚合的列,例如:
GROUP BY v_Collection_Alias.Name,
v_Package_Alias.Name,
v_Advertisement_Alias.CollectionID as CollectionID,
v_Advertisement_Alias.AdvertisementID as DeploymentID,
v_Advertisement_Alias.PresentTime as ModificationTime
或者,如果您不想维护冗长的GROUP BY子句,则可以从SELECT语句中删除SUM函数,并让报表处理聚合,并在报表生成器中进行适当的分组。
答案 1 :(得分:1)
第一次查询:
当您使用像SUM这样的聚合函数时,您需要在没有聚合函数的所有列上执行GROUP BY。
第二次查询:
这是有效的,因为您在选择时没有聚合函数,因此您不必使用GROUP BY,而报表生成器可以为您处理总和和分组。
第三个查询:
此查询有效,因为您在所有列上都有聚合函数,因此您不需要GROUP BY。
始终记住LOGICAL查询处理顺序如下:
1,从 2.WHERE 3.GROUP BY 4.HAVING 5.SELECT 6.ORDER BY
所以你的GROUP BY发生在SELECT之前因为那个原因你分组时你不能在你的group by子句中包含'',因为那个列还不存在。 所以它给你一个错误。