I have two tables that I want to query Customer and Service
Customer table
cnum lastName Address Phone Comments
2 McKenzie Main Street 1234567898 None
3 Stevenson South Street 1225448844 None
4 Adams North Street 1234545454 None
Service
IncidentNum cnum serviceDate status category LastUpdated
x1 2 02-21-2013 Closed Repair 02-21-2013
c2 2 05-12-2013 Open Installation 05-13-2013
d2 3 05-01-2013 Closed Repair 05-05-2013
f2 4 05-12-2013 Open Repair 05-12-2013
Basically what I want to do is display the records with latest update for each customer record, regardless if the status is Closed or Open.
Final:
cnum lastName Address Phone Category Last Service
2 McKenzie Main Street 1234567898 Installation 05-13-2013
3 Stevenson South Street 1225448844 Repair 05-05-2013
4 Adams North Street 1234545454 Repair 05-12-2013
Im using OLEDB, this is for my project in VB. Any input would be appreciated. Thank you in advance!
this is very simple.. see you have to join these two tables on * cnum *and then top of that you can put condition for latest/maximum servicedate..
You can use following query for the same task -
select customer.cnum,
customer.lastname,
customer.address,
customer.phone,
service.category,
service.servicedate
from customer,service
where customer.cnum = service.cnum
and service.servicedate = ( select max(serviceDate) from service service2
where service2.cnum = customer.cnum)
I hope this is what you are looking for..
Edit : Editing query to display the customer info which is not present in service table
select customer.cnum,
customer.lastname,
customer.address,
customer.phone,
service.category,
service.servicedate
from customer left outer join service on (customer.cnum = service.cnum)
where (service.servicedate = ( select max(serviceDate) from service service2
where service2.cnum = customer.cnum)
or service.cnum is null)
Can you please try this?
select c.cnum, c.lastname, c.address,
c.phone, s.category, s.servicedate
from customer c,service s
where c.cnum = s.cnum
group by c.cnum
having s.LastUpdated = max(s.LastUpdated)
select distinct customer.cnum,customer.lastname,customer.address,customer.phone,
service.category,
MAX(lastupdated)as lastservice from service,customer where service.cnum=customer.cnum
and service.servicedate = ( select max(serviceDate) from service service2
where service2.cnum = customer.cnum)
group by customer.cnum,customer.lastname,customer.address,customer.phone,
service.category
I hope this is what you are looking for. I tested it and it gives the exact output you mentioned in the question.
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.