Solved

MAX Number

Posted on 2007-11-26
4
265 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 142

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now