存储过程出错:无法应用内部连接

时间:2015-02-23 04:49:57

标签: sql-server stored-procedures

解决

  • 替换:vs.VehicleStatus VehicleStatus
  • 使用:max(vs.VehicleStatus) VehicleStatus

我是这个领域的新手,通过详细阐述这个问题给予了帮助。

错误:

  

程序sp_VRMS_VehicleRequest_getvehiclerequestsgrouped,第25行
  专栏' VRMS_VehicleStatus.VehicleStatus'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

存储过程查询:

SELECT max([VehicleRequestID]) VehicleRequestID
      ,[AdminId]
      ,max([DesireCarTypeId]) DesireCarTypeId
      ,max([AssignCarTypeId]) AssignCarTypeId
      ,max([Source]) Source 
      ,max([Destination]) Destination
      ,max([Landmark]) Landmark
      ,max([NumberOfPassenger]) NumberOfPassenger
      ,CASE WHEN max([PickUpTIme])= min([PickUpTIme]) THEN null ELSE max([PickUpTIme]) END PickUpTill
      ,min([PickUpTIme]) PickUpTIme
      ,[BookingDate]
      ,max([CarNumber]) CarNumber
      ,max([DriverName]) DriverName
      ,max([DriverMobile]) DriverMobile
      ,max([AllocationDate]) AllocationDate
      ,vs.VehicleStatus VehicleStatus
      ,max([Remarks]) Remarks
      ,max([CRFTRF]) CRFTRF
      ,max([CostCentreNo]) CostCentreNo
      ,max([DutyDetails]) DutyDetails
  FROM 
      [dbo].[VRMS_VehicleRequest] As vr 
  INNER JOIN 
      VRMS_VehicleStatus as vs on vr.VehicleStatusID = vs.VehicleStatusID 
  GROUP BY 
     AdminId, Bookingdate

3 个答案:

答案 0 :(得分:0)

您应该在group by子句中添加vs.VehicleStatus,如下所示:

group by AdminId,Bookingdate,vs.VehicleStatus

答案 1 :(得分:0)

“非分组列的聚合函数要求不是SQL的一般语法限制或SQL Server实现的任意语法限制!”这是简单的逻辑,就像除以零一样 - 它必须无论我们是写SQL还是手工制作,都要明确处理和处理......

以上是 this 文章的摘录。阅读它,它将回答你的问题。

答案 2 :(得分:0)

请注意,在应用GroupBy子句时,请确保select语句中的所有列都应该是聚合函数,即max,Min,Count,Sum,Avg等,或者它们应该在group by子句中。因此,您应该在group by子句中添加vs.VehicleStatus列,即Group By AdminId,BookingDate,vs.VehicleStatus。我希望这会有所帮助。

相关问题