Solved

MAX Number

Posted on 2007-11-26
4
301 Views
Last Modified: 2012-06-21
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
Comment
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
  • Learn & ask questions
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
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
            )

Open in new window

0
 
LVL 6

Expert Comment

by:Rajesh_mj
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 

Open in new window

0
 
LVL 11

Accepted Solution

by:
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

by:ziwez0
ID: 31410958
Thank you
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question