1

私はSQL構文を持っていますが、エラーが発生します:

メッセージ 8120、レベル 16、状態 1、行 1 列 'VEHICLEMASTR.cconduction' は、集計関数にも GROUP BY 句にも含まれていないため、選択リストでは無効です。

SQL 構文は次のとおりです。

SELECT A.cplateno,A.cconduction,A.cname,A.cbatterymodel
,A.dbattery,A.DlastChange,A.nlastoilkm,A.naveragekmday
,A.dkmreading,A.dfranacq,A.dfranexp,A.nlimit,A.dreading
,CONVERT(varchar(2),month(MAX(B.dinsexp)))+'/'+CONVERT(varchar(2),day(MAX(B.dinsexp)))+'/'+CONVERT(varchar(4),year(MAX(B.dinsexp))) as dinsexp
,C.corno,CONVERT(varchar(2),month(MAX(C.dregexp)))+'/'+CONVERT(varchar(2),day(MAX(C.dregexp)))+'/'+  CONVERT(varchar(4),year(MAX(C.dregexp))) as dregexp 
FROM VEHICLEMASTR A 
LEFT JOIN VEHICLEINSURANCE B 
ON A.cplateno = B.cplateno 
LEFT JOIN VREGISTRATION C 
ON A.cplateno = C.cplateno 
GROUP BY A.cplateno 

誰が何がうまくいかなかったのか教えてもらえますか?

4

3 に答える 3

2

The "group by" clause must name every column selected, except those columns that are aggregate functions.

FYI an "aggregate" function is one that returns a single value for many rows, like sum(), count(), etc

于 2013-06-29T05:03:29.560 に答える
0

a.cconduction needs to be in the group by clause.

When using a Group By clause, a column must either have an aggregate function (i.e. COUNT) or be defined in the group by.

A sample group by statement with multiple grouping:

SELECT column1_name, column2_name, aggregate_function(column_name3)
 FROM table_name
 WHERE column_name1 operator value
 GROUP BY column_name1, column_name2; 
于 2013-06-29T05:04:08.473 に答える
0

集計関数を持つ列を除いて、選択に言及したすべてのフィールドを含める必要があるため、この場合は次のようになります。

GROUP BY 
a.cplateno, a.cconduction,a.cname,a.cbatterymodel,a.dbattery,
a.DlastChange,a.nlastoilkm,a.naveragekmday, 
a.dkmreading,a.dfranacq,a.dfranexp,a.nlimit,a.dreading

それ以外の

GROUP BY a.cplateno

編集

のみが必要な場合は、次a.cplatenoを除く残りのフィールドを含めません。aggregate functiona.cplateno

SELECT  A.cplateno
,CONVERT(varchar(2),month(MAX(B.dinsexp)))+'/'+CONVERT(varchar(2),day(MAX(B.dinsexp)))+'/'+CONVERT(varchar(4),year(MAX(B.dinsexp))) as dinsexp 
,CONVERT(varchar(2),month(MAX(C.dregexp)))+'/'+CONVERT(varchar(2),day(MAX(C.dregexp)))+'/'+  CONVERT(varchar(4),year(MAX(C.dregexp))) as dregexp 
FROM VEHICLEMASTR A 
LEFT JOIN VEHICLEINSURANCE B 
ON A.cplateno = B.cplateno 
LEFT JOIN VREGISTRATION C 
ON A.cplateno = C.cplateno 
GROUP BY A.cplateno 
于 2013-06-29T05:07:55.470 に答える