2014-03-19 39 views
-2

我使用SQL Server 2008錯誤在SQL SELECT查詢的SQL Server 2008

我有,我想插入多行鍼對我的查詢是

insert into tbl select MAX(v.vehicle_id),3,v.category,150,v.rate 
from vehicle v where v.parent_id=3 and v.category_id=0 

我的選擇查詢表給錯誤

select MAX(v.vehicle_id),3,v.category,150,v.rate 
from vehicle v where v.parent_id=3 and v.category_id=0 

的錯誤是

Msg 8120, Level 16, State 1, Line 1 
Column 'vehicle.category' is invalid in the select list because it is not 
contained in either an aggregate function or the GROUP BY clause. 

我該如何解決這個問題?

+2

你有沒有嘗試使用GROUP BY? – MikkaRin

回答

0

使用此,Error is you are trying to select column name with aggregate function then need to group by clause

select MAX(v.vehicle_id),3,v.category,150,v.rate 
from vehicle v where v.parent_id=3 and v.category_id=0 group by v.category,v.rate 
0

要使用聚合函數,如MAX(),您在您查詢您所選擇的所有其他屬性的末尾應用GROUP BY

下面是一個例子結構:

SELECT MAX(attr1), attr2, attr3 
FROM table1 
WHERE attr2 = 'value' 
GROUP BY attr2, attr3; 
3
declare 
@vehical_id bigint; 

select @vehical_id=MAX(v.vehicle_id) 
from vehicle v where v.parent_id=3 and v.category_id=0 

insert into tbl select @vehical_id,3,v.category,150,v.rate 
from vehicle v where v.parent_id=3 and v.category_id=0