为什么此查询不起作用: -
SELECT b.byhub,
Count(awbno)
FROM (SELECT awbno,
byhub,
entrydatetime,
Max(entrydatetime)
FROM wbbooking
GROUP BY awbno,
byhub,
entrydatetime) B
INNER JOIN (SELECT refno,
Max(entrydatetime)
FROM wbmmreceiving
GROUP BY refno) R
ON B.awbno = R.refno
WHERE CONVERT(DATE, b.entrydatetime) BETWEEN '2016-10-13' AND '2016-10-13'
GROUP BY b.byhub
执行此查询后,我获得了此类错误: -
Msg 8155, Level 16, State 2, Line 2
No column name was specified for column 4 of 'b'.
Msg 8155, Level 16, State 2, Line 3
No column name was specified for column 2 of 'r'.
答案 0 :(得分:2)
发生错误,因为您没有为聚合列Max(entrydatetime)
指定名称。将其更改为Max(entrydatetime) AS max_entrydatetime
。
这个查询有点奇怪。子查询B
选择entrydatetime
以及Max(entrydatetime)
。但由于entrydatetime
已列在分组列表中,Max(entrydatetime)
将始终与entrydattetime
相同。
如果您想要按entrydatetime
和awbno
进行上一次byhub
,请不要按entrydatetime
进行分组,仅将其作为Max(entrydatetime)
包含在Max(entrydatetime)
中选择列表。如果您确实希望按此日期列进行分组,请不要将其包含在entrydatetime
中。
第二个子查询的目的是什么?除了加入之外,没有使用其返回的列。是否需要计算?
为什么在外部查询中包含where子句?如果将它包含在第一个子查询中,则效率更高,并且不再需要返回<form id= "checkboxes">
<label> Types of Places</label>
<link rel="stylesheet" type="text/css" href="css/style.css">
<input type="checkbox" name="cbox" value="woodland" class="places"/>
<label for="woodland"><img src="../img/Woodland.jpg" /></label>
<input type="checkbox" name="cbox" value="lodging" class="places"/>
<label for="viewpoint"><img src="../img/Viewpoint.jpg" /></label>
<input type="checkbox" name="cbox" value="restaurant" class="places"/>
<label for="restaurant"><img src="../img/Restaurant.jpg" /></label>
<input type="checkbox" name="cbox" value="museum" class="places" />
<label for="historical"><img src="../img/Historical.jpg" /></label>
</form>
列。
答案 1 :(得分:1)
只需为子查询指定列名称,如下所示:
SELECT b.byhub,
Count(awbno)
FROM (SELECT awbno,
byhub,
entrydatetime,
Max(entrydatetime) max_entrydatetime
FROM wbbooking
GROUP BY awbno,
byhub,
entrydatetime) B
INNER JOIN (SELECT refno,
Max(entrydatetime) max_entrydatetime
FROM wbmmreceiving
GROUP BY refno) R
ON B.awbno = R.refno
WHERE CONVERT(DATE, b.entrydatetime) BETWEEN '2016-10-13' AND '2016-10-13'
GROUP BY b.byhub