Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Querying related to grouping

Status
Not open for further replies.

paradis222

Programmer
Oct 20, 2002
12
0
0
HK
I have a question related to grouping data
I have 2 fields, id and Serial No. id is unique and Serial No can be duplicate
the query like this:

<cfquery name=&quot;recordcheck&quot; datasource=&quot;support&quot;>
select Max(id) as tempid, Serialno from loan where Serialno IN (#QuotedValueList(tracking.Serialno)#) group by Serialno
</cfquery>

I would like to select all record in table &quot;loan&quot;, if there is duplicate serial no in the table, then select serial no with a large value of &quot;id&quot; , how can i do that? thanks!
 
use a correlated subquery to get the largest id for each serialno group --

[tt]select Serialno, id
from loan X
where id =
( select max(id)
from loan
where Serialno = X.Serialno )
and Serialno in
(#QuotedValueList(tracking.Serialno)#)[/tt]

rudy
 
I think I should amend this query... but how can i amend for the same result and function?

I would like to select all record in table &quot;serialrecord&quot;, if there is duplicate serial no in the &quot;loan&quot; table, then select serial no with a large value of &quot;id&quot;

<cfquery name=&quot;serial&quot; datasource=&quot;support&quot;>
SELECT DISTINCT serialrecord.Serialno AS SN, serialrecord.Partno as PN, serialrecord.Description as Des, serialrecord.Vendor AS Ven, serialrecord.SystemID as Sys, serialrecord.Po as Porder, serialrecord.Vendorwarranty as Venwar, claim.Serialno, claim.replacedsn as repsn, claim.rma as rmano, claim.replacedpartno as repart,loan.Serialno, loan.id, loan.status as newstatus from ((serialrecord left outer join claim on serialrecord.Serialno = claim.Serialno)left outer join loan on serialrecord.Serialno = loan.Serialno )where serialrecord.Serialno IN (#QuotedValueList(tracking.Serialno)#)
</cfquery>

Thankss!!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top