我有这个问题:
SELECT dbo.scheduledatesff.job_no,
dbo.scheduledatesff.contractreceived_f,
dbo.scheduledatesff.designreview_f,
dbo.scheduledatesss.orderplotplan_s,
dbo.scheduledatesff.orderplotplan_f,
dbo.scheduledatesss.ordertrusses_s,
dbo.scheduledatesff.ordertrusses_f,
dbo.scheduledatesss.orderheatcalcs_s,
dbo.scheduledatesff.orderheatcalcs_f,
dbo.scheduledatesss.permitrelease_s,
dbo.scheduledatesff.permitrelease_f,
dbo.scheduledatesff.intselectionsappt_f,
dbo.scheduledatesss.excavationfile_s,
dbo.scheduledatesff.excavationfile_f,
dbo.scheduledatesss.framingfile_s,
dbo.scheduledatesff.framingfile_f,
dbo.scheduledatesss.finishingfile_s,
dbo.scheduledatesff.finishingfile_f,
dbo.scheduledatesff.startdate,
dbo.scheduledatesff.possession,
dbo.tblcustomers.expected_occupancy,
dbo.maxscheddate.description,
dbo.maxscheddate.actualfinish,
dbo.tblprojectmanager.pmname,
dbo.airport_insert.engineeringreq,
dbo.airport_insert.engineeringrec,
dbo.airport_insert.appforms,
dbo.airport_insert.cheque,
dbo.airport_insert.readytosubmit,
dbo.airport_insert.deficiencyreq,
dbo.airport_insert.deficiencyresp,
dbo.pmcolours.pmcolour
FROM dbo.scheduledatesff
INNER JOIN dbo.tblcustomers
ON dbo.scheduledatesff.job_no = dbo.tblcustomers.job_no
INNER JOIN dbo.scheduledatesss
ON dbo.scheduledatesff.job_no =
dbo.scheduledatesss.job_no
INNER JOIN dbo.tbljobs
ON dbo.scheduledatesff.job_no = dbo.tbljobs.job_no
LEFT OUTER JOIN dbo.tblprojectmanager
ON dbo.tbljobs.estimator = dbo.tblprojectmanager.pm
LEFT OUTER JOIN dbo.pmcolours
ON dbo.tblprojectmanager.pm = dbo.pmcolours.pmid
LEFT OUTER JOIN dbo.maxscheddate
ON dbo.scheduledatesff.job_no =
dbo.maxscheddate.job_no
LEFT OUTER JOIN dbo.airport_insert
ON dbo.scheduledatesff.job_no =
dbo.airport_insert.job_no
GROUP BY dbo.scheduledatesff.job_no
当我取出该组时,它工作正常,但是我的小组得到了这个错误:
列'dbo.scheduledatesff.ContractReceived_F'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
为什么我收到此错误,我该如何解决?我试图按bo.scheduledatesff.job_no
分组,我认为这就像添加一个简单的GROUP BY
一样简单,我错了。
我要做的就是按job_no
分组,所以我没有任何重复,DISTINCT
我仍然会重复。
答案 0 :(得分:3)
在选择列表中(即您在SELECT
关键字后列出的所有列),当您与GROUP BY
聚合时,您只能在{{1}中指定任一列本身或列的聚合(例如,GROUP BY
)。
这样做的原因是,任何未被分组但未被聚合的列都必须是来自行的单个值,但每个组可能有多行。那你用哪一排?在MySQL中,它只是使用第一行,但不能保证。在SQL Server中,您必须是显式的。您需要告诉它如何获取您没有分组的列的值。
这对您来说意味着您需要考虑您希望结果的样子。对于每个工作,您想要各种订单项目的总数吗?然后在这些列上使用SUM()
。那样做的是为该组中的每一行(即,属于同一SUM()
的每一行)总结该列的值,结果将是该总和,而不是单行&# 39; s值。
job_no
,因为由于加入问题而导致重复行。如果是这种情况,请更新您的问题,以便我们为您提供帮助。
答案 1 :(得分:0)
SELECT dbo.scheduledatesff.job_no,
max(dbo.scheduledatesff.contractreceived_f),
max(dbo.tblcustomers.expected_occupancy)
...
FROM dbo.scheduledatesff
JOIN dbo.tblcustomers
ON dbo.scheduledatesff.job_no = dbo.tblcustomers.job_no
...
GROUP BY dbo.scheduledatesff.job_no