SQL选择列表中的列 'PB_Station.ID' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中,该如何处理
2012-04-14
SQL选择列表中的列 PB_Station.ID 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中SQL codeALTER pr
SQL选择列表中的列 'PB_Station.ID' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中
SQL code
ALTER procedure [dbo].[PB_GetStationList]@StaionName nvarchar(50),@AreaID int,@StartDate datetime,@EndDate datetime,@StartIndex int,@PageSize intasselect * from( select top (@StartIndex+@PageSize) ROW_NUMBER() over (order by PB_Station.ID ASC) as XUEHAO, Area.AreaName, PB_Station.StationName,PB_Station.StationNo, SUM(case when LeaseOut.TimeIn is null then 1 else 0 end) as OutNum, SUM(case when LeaseIn.TimeIn is not null then 1 else 0 end) as InNum from PB_Station inner join PB_Area Area on Area.ID=PB_Station.Area inner join PB_MgrBox MgrBox on MgrBox.Station=PB_Station.ID inner join PB_BPillar BPillar on BPillar.MgrBox=MgrBox.ID left join PB_Lease LeaseOut on LeaseOut.BPillarIDOut=BPillar.ID left join PB_Lease LeaseIn on LeaseIn.BPillarIDIn=BPillar.ID where ( PB_Station.StationName like '%'+@StaionName+'%' and Area.ID=@AreaID and DATEDIFF(day,LeaseOut.TimeOut,@StartDate)<=0 and DATEDIFF(day,LeaseOut.TimeOut,@EndDate)>=0 or DATEDIFF(day,LeaseIn.TimeIn,@StartDate)<=0 and DATEDIFF(day,LeaseIn.TimeIn,@EndDate)>=0 ))twhere XUEHAO>@StartIndex
ALTER procedure [dbo].[PB_GetStationList] @StaionName nvarchar(50), @AreaID int, @StartDate datetime, @EndDate datetime, @StartIndex int, @PageSize int as select * from ( select top (@StartIndex+@PageSize) ROW_NUMBER() over (order by PB_Station.ID ASC) as XUEHAO, Area.AreaName, PB_Station.StationName,PB_Station.StationNo, SUM(case when LeaseOut.TimeIn is null then 1 else 0 end) as OutNum, SUM(case when LeaseIn.TimeIn is not null then 1 else 0 end) as InNum from PB_Station inner join PB_Area Area on Area.ID=PB_Station.Area inner join PB_MgrBox MgrBox on MgrBox.Station=PB_Station.ID inner join PB_BPillar BPillar on BPillar.MgrBox=MgrBox.ID left join PB_Lease LeaseOut on LeaseOut.BPillarIDOut=BPillar.ID left join PB_Lease LeaseIn on LeaseIn.BPillarIDIn=BPillar.ID where ( PB_Station.StationName like '%'+@StaionName+'%' and Area.ID=@AreaID and DATEDIFF(day,LeaseOut.TimeOut,@StartDate)<=0 and DATEDIFF(day,LeaseOut.TimeOut,@EndDate)>=0 or DATEDIFF(day,LeaseIn.TimeIn,@StartDate)<=0 and DATEDIFF(day,LeaseIn.TimeIn,@EndDate)>=0 ) group by Area.AreaName,PB_Station.StationName,PB_Station.StationNo )t where XUEHAO>@StartIndex
sum为聚合函数,和它一起出现的列必须包含在group by 后面 [解决办法]
[解决办法]
[解决办法] SUM(case when LeaseOut.TimeIn is null then 1 else 0 end) OVER (PARTITION BY Area.AreaName) [解决办法] 两句SUM()你都可以这么搞。