SQL选择列表中的列 'PB_Station.ID' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中,该如何处理
SQL选择列表中的列 'PB_Station.ID' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中,该如何处理
日期:2014-05-18 浏览次数:20581 次
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 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
)
)t
where 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()你都可以这么搞。