Sql Server:如何在WHERE子句中使用像MAX这样的聚合函数

时间:2022-11-09 22:34:14

I want get the maximum value for this record. Please help me:

我想获得此记录的最大值。请帮帮我:

SELECT rest.field1 
    FROM mastertable AS m
    INNER JOIN  (
        SELECT t1.field1 field1, 
               t2.field2
            FROM table1 AS T1 
            INNER JOIN table2 AS t2 ON t2.field = t1.field 
            WHERE t1.field3=MAX(t1.field3)
        --                  ^^^^^^^^^^^^^^  Help me here.
    ) AS rest ON rest.field1 = m.field

6 个解决方案

#1


18  

You could use a sub query...

您可以使用子查询...

WHERE t1.field3 = (SELECT MAX(st1.field3) FROM table1 AS st1)

But I would actually move this out of the where clause and into the join statement, as an AND for the ON clause.

但我实际上将它从where子句移出到join语句中,作为ON子句的AND。

#2


35  

As you've noticed, the WHERE clause doesn't allow you to use aggregates in it. That's what the HAVING clause is for.

正如您所注意到的,WHERE子句不允许您在其中使用聚合。这就是HAVING子句的用途。

HAVING t1.field3=MAX(t1.field3)

#3


6  

The correct way to use max in the having clause is by performing a self join first:

在having子句中使用max的正确方法是首先执行自连接:

select t1.a, t1.b, t1.c
from table1 t1
join table1 t1_max
  on t1.id = t1_max.id
group by t1.a, t1.b, t1.c
having t1.date = max(t1_max.date)

The following is how you would join with a subquery:

以下是如何加入子查询:

select t1.a, t1.b, t1.c
from table1 t1
where t1.date = (select max(t1_max.date)
                 from table1 t1_max
                 where t1.id = t1_max.id)

Be sure to create a single dataset before using an aggregate when dealing with a multi-table join:

在处理多表连接时,请确保在使用聚合之前创建单个数据集:

select t1.id, t1.date, t1.a, t1.b, t1.c
into #dataset
from table1 t1
join table2 t2
  on t1.id = t2.id
join table2 t3
  on t1.id = t3.id


select a, b, c
from #dataset d
join #dataset d_max
  on d.id = d_max.id
having d.date = max(d_max.date)
group by a, b, c

Sub query version:

子查询版本:

select t1.id, t1.date, t1.a, t1.b, t1.c
into #dataset
from table1 t1
join table2 t2
  on t1.id = t2.id
join table2 t3
  on t1.id = t3.id


select a, b, c
from #dataset d
where d.date = (select max(d_max.date)
                from #dataset d_max
                where d.id = d_max.id)

#4


3  

SELECT rest.field1
FROM mastertable as m
INNER JOIN table1 at t1 on t1.field1 = m.field
INNER JOIN table2 at t2 on t2.field = t1.field
WHERE t1.field3 = (SELECT MAX(field3) FROM table1)

#5


1  

yes you need to use a having clause after the Group by clause , as the where is just to filter the data on simple parameters , but group by followed by a Having statement is the idea to group the data and filter it on basis of some aggregate function......

是的,您需要在Group by子句之后使用having子句,因为where只是过滤简单参数的数据,但group by后跟Have语句是分组数据并根据某些聚合过滤它的想法功能......

#6


0  

But its still giving an error message in Query Builder. I am using SqlServerCe 2008.

但它仍然在Query Builder中给出错误消息。我正在使用SqlServerCe 2008。

SELECT         Products_Master.ProductName, Order_Products.Quantity, Order_Details.TotalTax, Order_Products.Cost, Order_Details.Discount, 
                     Order_Details.TotalPrice
FROM           Order_Products INNER JOIN
                     Order_Details ON Order_Details.OrderID = Order_Products.OrderID INNER JOIN
                     Products_Master ON Products_Master.ProductCode = Order_Products.ProductCode
HAVING        (Order_Details.OrderID = (SELECT MAX(OrderID) AS Expr1 FROM Order_Details AS mx1))

I replaced WHERE with HAVING as said by @powerlord. But still showing an error.

正如@powerlord所说,我用HAVING替换了WHERE。但仍然显示错误。

Error parsing the query. [Token line number = 1, Token line offset = 371, Token in error = SELECT]

解析查询时出错。 [令牌行号= 1,令牌行偏移= 371,令牌错误= SELECT]

#1


18  

You could use a sub query...

您可以使用子查询...

WHERE t1.field3 = (SELECT MAX(st1.field3) FROM table1 AS st1)

But I would actually move this out of the where clause and into the join statement, as an AND for the ON clause.

但我实际上将它从where子句移出到join语句中,作为ON子句的AND。

#2


35  

As you've noticed, the WHERE clause doesn't allow you to use aggregates in it. That's what the HAVING clause is for.

正如您所注意到的,WHERE子句不允许您在其中使用聚合。这就是HAVING子句的用途。

HAVING t1.field3=MAX(t1.field3)

#3


6  

The correct way to use max in the having clause is by performing a self join first:

在having子句中使用max的正确方法是首先执行自连接:

select t1.a, t1.b, t1.c
from table1 t1
join table1 t1_max
  on t1.id = t1_max.id
group by t1.a, t1.b, t1.c
having t1.date = max(t1_max.date)

The following is how you would join with a subquery:

以下是如何加入子查询:

select t1.a, t1.b, t1.c
from table1 t1
where t1.date = (select max(t1_max.date)
                 from table1 t1_max
                 where t1.id = t1_max.id)

Be sure to create a single dataset before using an aggregate when dealing with a multi-table join:

在处理多表连接时,请确保在使用聚合之前创建单个数据集:

select t1.id, t1.date, t1.a, t1.b, t1.c
into #dataset
from table1 t1
join table2 t2
  on t1.id = t2.id
join table2 t3
  on t1.id = t3.id


select a, b, c
from #dataset d
join #dataset d_max
  on d.id = d_max.id
having d.date = max(d_max.date)
group by a, b, c

Sub query version:

子查询版本:

select t1.id, t1.date, t1.a, t1.b, t1.c
into #dataset
from table1 t1
join table2 t2
  on t1.id = t2.id
join table2 t3
  on t1.id = t3.id


select a, b, c
from #dataset d
where d.date = (select max(d_max.date)
                from #dataset d_max
                where d.id = d_max.id)

#4


3  

SELECT rest.field1
FROM mastertable as m
INNER JOIN table1 at t1 on t1.field1 = m.field
INNER JOIN table2 at t2 on t2.field = t1.field
WHERE t1.field3 = (SELECT MAX(field3) FROM table1)

#5


1  

yes you need to use a having clause after the Group by clause , as the where is just to filter the data on simple parameters , but group by followed by a Having statement is the idea to group the data and filter it on basis of some aggregate function......

是的,您需要在Group by子句之后使用having子句,因为where只是过滤简单参数的数据,但group by后跟Have语句是分组数据并根据某些聚合过滤它的想法功能......

#6


0  

But its still giving an error message in Query Builder. I am using SqlServerCe 2008.

但它仍然在Query Builder中给出错误消息。我正在使用SqlServerCe 2008。

SELECT         Products_Master.ProductName, Order_Products.Quantity, Order_Details.TotalTax, Order_Products.Cost, Order_Details.Discount, 
                     Order_Details.TotalPrice
FROM           Order_Products INNER JOIN
                     Order_Details ON Order_Details.OrderID = Order_Products.OrderID INNER JOIN
                     Products_Master ON Products_Master.ProductCode = Order_Products.ProductCode
HAVING        (Order_Details.OrderID = (SELECT MAX(OrderID) AS Expr1 FROM Order_Details AS mx1))

I replaced WHERE with HAVING as said by @powerlord. But still showing an error.

正如@powerlord所说,我用HAVING替换了WHERE。但仍然显示错误。

Error parsing the query. [Token line number = 1, Token line offset = 371, Token in error = SELECT]

解析查询时出错。 [令牌行号= 1,令牌行偏移= 371,令牌错误= SELECT]