简体   繁体   English

在sql中使用聚合函数

[英]Using Aggregate Function in sql

Why this query is not working:- 为什么此查询不起作用: -

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 

After Execute this query I have obtain this type of Error:- 执行此查询后,我已获得此类错误: -

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'.

The errors occur, because you have not specified a name for the aggregate column Max(entrydatetime) . 发生错误,因为您没有为聚合列Max(entrydatetime)指定名称。 Change it to Max(entrydatetime) AS max_entrydatetime . 将其更改为Max(entrydatetime) AS max_entrydatetime

This query is a bit strange. 这个查询有点奇怪。 The subquery B selects entrydatetime as well as Max(entrydatetime) . 子查询B选择entrydatetime以及Max(entrydatetime) But since entrydatetime is included in the group by list, Max(entrydatetime) will always be the same as entrydattetime . 但由于entrydatetime包含在group by list中,因此Max(entrydatetime)将始终与entrydattetime相同。

If you want the last entrydatetime per awbno and byhub , then don't group by entrydatetime and only include it as Max(entrydatetime) in the select list. 如果您想要每个awbnobyhub的最后一个entrydatetime ,那么不要按entrydatetime进行分组,只在选择列表中将其包含为Max(entrydatetime) If you really want to group by this date column, then don't include it as Max(entrydatetime) . 如果您确实希望按此日期列进行分组,则不要将其包含为Max(entrydatetime)

What is the purpose of the second subquery? 第二个子查询的目的是什么? None of its returned columns are used except for joining. 除了加入之外,没有使用其返回的列。 Is it needed for the count? 伯爵需要它吗?

Why include a where-clause in the outer query? 为什么在外部查询中包含where子句? If you include it in the first subquery, it is more efficient and the entrydatetime column needs not to be returned any more. 如果将它包含在第一个子查询中,则效率更高,并且不再需要返回entrydatetime列。

Just specify column names for your subqueries, like this: 只需为子查询指定列名称,如下所示:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM