[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MAX Number

Posted on 2007-11-26
4
Medium Priority
?
317 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
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 2000 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

873 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