33

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
dlamblin
  • 43,965
  • 20
  • 101
  • 140
  • 1
    I'm no SQL guru but does that work or not? You don't even state what you're having trouble with and whether or not your posted solution works or not. – Nick Bedford Sep 25 '09 at 05:42
  • You are going to have to explain what you want better for me to understand this question. – tster Sep 25 '09 at 05:42
  • is there any solution to get the proper result? –  Sep 25 '09 at 06:16

6 Answers6

44

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

HAVING t1.field3=MAX(t1.field3)
Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 3
    As a side note (5+ years later), the `HAVING` clause is actually intended for use with grouped elements. However, it works with aggregate clauses (`MIN`, `MAX`, `AVG`) in most SQL engines even without a `GROUP BY` clause. – Powerlord Mar 23 '15 at 17:14
36

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.

dlamblin
  • 43,965
  • 20
  • 101
  • 140
14

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

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)
metrix
  • 1,486
  • 2
  • 11
  • 19
5
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)
Tim Santeford
  • 27,385
  • 16
  • 74
  • 101
1

But its still giving an error message in Query Builder. I am using 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.

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

Kamal
  • 469
  • 2
  • 8
  • 19
  • I think you can simplify the HAVING clause here to just `HAVING Order_Details.OrderID = MAX(Order_Details.OrderID)` – Powerlord Nov 26 '13 at 15:33
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......

Rohit
  • 330
  • 1
  • 5
  • 16