/ / SQL: Zapytaj 2 tabele, aby uzyskać najnowszą datę dla każdego rekordu - sql, vb.net, ms-access

SQL: Zapytaj 2 tabele, aby uzyskać najnowszą datę dla każdego rekordu - sql, vb.net, ms-access

Mam dwie tabele, które chcę przesłać do klienta i obsługi

Tabela klientów

    cnum       lastName        Address       Phone        Comments

2         McKenzie        Main Street   1234567898   None
3         Stevenson       South Street  1225448844   None
4         Adams           North Street  1234545454   None

Usługa

     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

Zasadniczo chcę wyświetlić rekordy z najnowszą aktualizacją dla każdego rekordu klienta, niezależnie od tego, czy status jest Zamknięty czy Otwarty.

Finał:

    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

Korzystam z OLEDB, to jest mój projekt w VB. Wszelkie uwagi będą mile widziane. Z góry dziękuję!

Odpowiedzi:

0 dla odpowiedzi № 1

Czy możesz tego spróbować?

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)

0 dla odpowiedzi nr 2
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

Mam nadzieję, że tego właśnie szukasz. Przetestowałem to i daje dokładny wynik wymieniony w pytaniu.


0 dla odpowiedzi № 3

to jest bardzo proste .. musisz połączyć te dwie tabele na *cnum* a na dodatek możesz ustawić warunek dla najnowszego / maksymalnego serwisu.

Możesz użyć następującego zapytania dla tego samego zadania -

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)

Mam nadzieję, że tego właśnie szukasz ...

Edytować: Edycja zapytania w celu wyświetlenia informacji o kliencie, których nie ma w tabeli usług

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)