简体   繁体   中英

How to get one row from duplicated rows?

I have 2 tables SVC_ServiceTicket and SVC_CustomersVehicle

The table ServiceTicket has a column customerID which is a foreign key to CustomersVehicle.So in ServiceTicket column customerID can have duplicate values.

When I do

select sst.ServiceTicketID,sst.CustomerID 
from ServiceTicket sst,CustomersVehicle scv 
where sst.CustomerID=scv.CV_ID

then it gives me duplicate customerID.So my requirement is if there are duplicate values of customerID then I want the latest customerID and as well serviceticket of that corresponding(latest customerID)

For example in the below screenshot there are customerID 13 is repeating so in this case I want latest customerID as well as serviceticket so the values I want is 8008 and 13

Please tell me how to do

在此处输入图片说明

Use aggregate function MAX . Also I would recommend to use a JOIN .

SELECT MAX(sst.ServiceTicketID) AS ServiceTicketID,sst.CustomerID 
FROM ServiceTicket sst JOIN
     CustomersVehicle scv ON sst.CustomerVehicleID=scv.CV_ID
GROUP BY sst.CustomerID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM