Solved

MAX Number

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Datetime function 12 81
tempdb latch contention 12 47
Help with SQL Server Stoplist 2 15
Azure SQL Insert not working suddenly 11 18
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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 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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

948 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

22 Experts available now in Live!

Get 1:1 Help Now