无法解决“无法使用GROUP BY子句列表中用于组的表达式中的聚合或子查询”错误

时间:2017-04-04 04:43:24

标签: sql

这个错误的一些答案表明我不能在没有聚合函数的情况下使用dm.DonarName,但不确定在这种特定情况下我该怎么办呢?

查看另一个解决方案(Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause),我尝试使用MAX(dm.DonorMasterId)而不是dm.DonarNamer,但它仍然不起作用。

{{1}}

1 个答案:

答案 0 :(得分:0)

最简单快速的解决方法是使用聚合函数:

select   aa.Funder
    ,plgm.ProgramLaunchID 
    ,sm_tmo.FirstName + ' ' + sm_tmo.LastName as "TMO"  
    ,d.DistrictName as "Location"
    ,CONVERT(VARCHAR(12), pl.StartDate, 107) as "Project start date"
    ,COUNT (distinct plgm.GroupID) as "Target groups"
    ,SUM(case when plgm.ActivationDate IS NULL then 0 else 1 end) as "Active groups"
    ,COUNT (distinct plgm.GroupID) * plsm.TotalNosesssion as "Target (year)"
    ,'-' as "Target (YTD)"
    ,COUNT (a.TMOID) as "Actual"        

from    dbo.PLGroupMapping plgm
    ,dbo.ProgramLaunch pl
    ,dbo.ProgramMaster pm
     LEFT JOIN 
(
  SELECT pmf2.ProgramMasterID, STUFF(( SELECT distinct ','+ dm.DonarName
                FROM    dbo.DonarMaster dm
                JOIN dbo.PMFunder pmf on pmf.FunderId = dm.DonarMasterID
                WHERE   pmf.ProgramMasterID = pfm2.ProgramMasterID
                FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, '') as Funder
  FROM dbo.PMFunder pmf2 
  GROUP BY pmf2.ProgramMasterID
) aa ON aa.ProgramMasterID = pm.ProgramMasterID
    ,dbo.PMFunder pmf
    ,dbo.DonarMaster dm 
    ,dbo.District d
    ,Magicbus.dbo.PLSessionMapping plsm 
    ,dbo.StaffMaster sm_tmo
    LEFT JOIN #r6a a
    on a.TMOID = sm_tmo.StaffMasterID

where       plgm.ProgramLaunchid = pl.ProgramLaunchID
and         pl.ProgramMasterID = pm.ProgramMasterID
and         pm.ProgramMasterID = pmf.ProgramMasterID
and         pmf.FunderId = dm.DonarMasterID
and         plgm.TMOID = sm_tmo.StaffMasterID
and         pl.DistrictID = d.DistrictID
and         plsm.ProgramLaunchID = plgm.ProgramLaunchID

group by aa.Funder
        ,plgm.ProgramLaunchID
        ,sm_tmo.FirstName + ' ' + sm_tmo.LastName
        ,d.DistrictName
        ,pl.StartDate
        ,plsm.TotalNosesssion
order by    COUNT (a.TMOID), plgm.ProgramLaunchID

我认为更好的方法是将相关子查询移动到LEFT JOIN,如下所示:

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope) {
  $scope.mainMenu = {};
  $scope.submenu = {};
  $scope.pagemenu ={};
  $scope.menu   = {};
  
  $scope.menus  = [
    {"menuID":"11","sub_menu":"N","name":"dashboard","sub_menus":""},
    {"menuID":"1","sub_menu":"Y","name":"settings","sub_menus":[{"sub_menuID":"1","name":"settings1","page":"Y","pages":[{"pageID":"1","name":"page1"},{"pageID":"2","name":"page2"}]},{"sub_menuID":"2","name":"settings2","page":"N","pages":""}]},
    {"menuID":"2","sub_menu":"Y","name":"help","sub_menus":[{"sub_menuID":"3","name":"help1","page":"N","pages":""},{"sub_menuID":"4","name":"help2","page":"N","pages":""}]},
    {"menuID":"3","sub_menu":"Y","name":"contact","sub_menus":[{"sub_menuID":"5","name":"contact1","page":"N","pages":""},{"sub_menuID":"6","name":"contact2","page":"N","pages":""}]}
  ];
  
  $scope.assignValue = function(menuId,submenuId,pageId){
    /* if(!$scope.mainMenu[menuId]&&!$scope.submenu[menuId]&&!$scope.pagemenu[menuId]){
      delete($scope.mainMenu[menuId]);
      delete($scope.submenu[menuId]);
      delete($scope.pagemenu[menuId]);
    } */
    
    $scope.menu=Object.assign({},$scope.mainMenu, $scope.submenu,$scope.pagemenu);
    console.log($scope.menu);
  }
  
  $scope.submit = function(){
    // alert(JSON.stringify($scope.menu));
    console.log(JSON.stringify($scope.menu));
    console.log($scope.menu);
  }
  
});