Click here to Skip to main content
15,924,317 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get Id of last journey vehicle. But subquery gives the error
C#
"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."


How solve this problem.

What I have tried:

SQL
select [dbo_pibm].[tbl_Vehicle].Id,
[dbo_pibm].[tbl_Vehicle].[Vehicle_Type],
[dbo_pibm].[tbl_Vehicle].Vehicle_No,
[dbo_pibm].[tbl_User].User_firstname+' '+[dbo_pibm].[tbl_User].User_surname as driver,[dbo_pibm].[tbl_Vehicle_Journey].Start_Place + ' To ' +[dbo_pibm].[tbl_Vehicle_Journey].End_Place as Journey,
 CONVERT(VARCHAR(8),Start_Time,108) as Start_Time,
 CONVERT(VARCHAR(8),End_Time,108)as End_Time,CONVERT(VARCHAR(108),
 Journey_Date,103) as Journey_Date,
 [dbo_pibm].[tbl_Vehicle_Journey].Id as TravelId

  
 from [dbo_pibm].[tbl_Vehicle]
 left outer join  [dbo_pibm].[tbl_Vehicle_Journey] 
 on [dbo_pibm].[tbl_Vehicle].Id=[dbo_pibm].[tbl_Vehicle_Journey].Vehicle_Id
 left outer join [dbo_pibm].[tbl_User]
 on [dbo_pibm].[tbl_User].User_id=[dbo_pibm].[tbl_Vehicle_Journey].Driver
  where [dbo_pibm].[tbl_Vehicle].[Id]=(select  Vehicle_Id  from [dbo_pibm].[tbl_Vehicle_Journey] 
 group by Vehicle_Id HAVING MAX(Id)>0)
Posted
v3

1 solution

So, I guess the below subquery is the problem.
SQL
select  Vehicle_Id  from [dbo_pibm].[tbl_Vehicle_Journey] group by Vehicle_Id HAVING MAX(Id)>0

Run this independently and see how many results are coming. If more than one are coming, then this is the issue. Make it correct to return one value only.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900