Solved

# MAX Number

Posted on 2007-11-26
306 Views
Morning,

Im having a bad morning, im trying to return 1 record..

SELECT DISTINCT SerialNo, MAX(DISTINCT PartNo) AS PartNo, MAX(DISTINCT ShippedDate) AS ShippedDate, fdescript, fcusrchr1
FROM         MACHINESEARCH
GROUP BY SerialNo, PartNo, ShippedDate, fdescript, fcusrchr1
HAVING      (PartNo LIKE @PartNo%) AND (SerialNo = @SerialNo

the issue i have it returns 2+ records,
SerialNo | PartNo      | ShippedDate | fdescript | fcusrch1
-----------------------------------------------------------------
00010       12435-01    25/10/2007     abc            12
00010       12435-02    27/10/2007     addwww   12          <----------- i want this row only

I only want 1 row with the highest(max) PartNo
can someone show me how to do this please
0
Question by:ziwez0
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 143

Expert Comment

ID: 20349347

``````SELECT t.serialno, t.partno, t.shippeddate, t.fdescript, f.fcusrchr1
FROM         MACHINESEARCH t
WHERE (t.PartNo LIKE @PartNo%)
AND (t.SerialNo = @SerialNo
and t.shippeddate = ( Select max(i.shippeddate)
from MACHINESEARCH i
WHERE (i.PartNo LIKE @PartNo%)
AND (i.SerialNo = @SerialNo
)
``````
0

LVL 6

Expert Comment

ID: 20349433

``````SELECT TOP 1 SerialNo,  PartNo AS PartNo,  ShippedDate AS ShippedDate, fdescript, fcusrchr1
FROM         MACHINESEARCH
WHERE      (PartNo LIKE @PartNo%) AND (SerialNo = @SerialNo)
ORDER BY ShippedDate DESC
``````
0

LVL 11

Accepted Solution

yuching earned 500 total points
ID: 20355884
SELECT SerialNo, PartNo, ShippedDate, fdescript, fcusrchr1
FROM         MACHINESEARCH a
INNER JOIN (
SELECT SerialNo, Max(PartNo)  As MaxPartNo
FROM    MACHINESEARCH
WHERE PartNo LIKE @PartNo% AND SerialNo = @SerialNo
GROUP BY SerialNo
) b ON a.SerialNo = b.SerialNo and a.PartNo = b.MaxPartNo

GROUP BY SerialNo, PartNo, ShippedDate, fdescript, fcusrchr1
0

LVL 2

Author Closing Comment

ID: 31410958
Thank you
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
###### Suggested Courses
Course of the Month7 days, 16 hours left to enroll