Question

I am trying to create a query that returns the accountservice_id with the MAX fromdate for each service_id. Each service_id can have multiple accountservice_id's tied to it and unfortunantly the MAX accountservice_id doesn't always have the MAX fromdate.

For example:

service_id   accountservice_id       fromdate
---------------------------------------------------
3235           1081         2009-12-01 12:00:00
3235           1007         2013-03-15 12:00:00
3235           2104         2012-10-25 12:00:00
3340           1047         2009-12-15 13:50:00

Below is my current query.

SELECT  service.service_id, accountservice.accountservice_id, accountservice.fromdate
FROM    service 
INNER JOIN accountservice ON service.service_id = accountservice.service_id
WHERE (service.servicetype_id IN (1, 74571, 74566))
ORDER BY service.service_id, accountservice.fromdate
Was it helpful?

Solution 2

You can do subqueries to get the data. Use one query to get the maximum fromdate for the service_id, then join that to a search for the accountservice_id that matches the service_id and max fromdate.

SELECT maxfromdate.service_id, correct_account.accountservice_id, maxfromdate.maxfromdate
FROM (SELECT 
    Service.service_id, 
    MAX (accountservice.fromdate) AS maxfromdate 
    FROM service 
    JOIN accountservice ON service.service_id = accountservice.service_id) 
    GROUP BY Service.service_id) maxfromdate
JOIN (SELECT 
    Service.service_id, 
    accountservice.accountservice_id, 
    accountservice.fromdate 
    FROM service 
    JOIN accountservice ON service.service_id = accountservice.service_id
)correct_account ON
     (maxfromdate.service_id = correct_account.service_id 
      AND maxfromdate.maxfromdate = correct_account.fromdate)

OTHER TIPS

CTE + ROW_NUMBER() is a winning combo.

;with cte as (
   SELECT service_id,
          accountservice_id,
          max_date_rank = row_number() 
                          over(partition by service_id 
                               order by fromdate desc)
   FROM   service
)
SELECT *
FROM   cte
WHERE  max_date_rank = 1

ETA: With a derived table:

SELECT * 
FROM (
    SELECT service_id,
           accountservice_id,
           max_date_rank = row_number() 
                           over(partition by service_id 
                                order by fromdate desc)
    FROM   service
) t
WHERE t.max_date_rank = 1

You can create a temp table to get the fromdate of service_id first:

Select service.service_id, max(accountService.fromdate) fromdate
into #serviceMaxDate from service 
inner join accountService ON service.service_id = accountservice.service_id
group by service.service_id

After, just get it:

SELECT  service.service_id, accountservice.accountservice_id, service.fromdate
FROM    #serviceMaxDate service
INNER JOIN accountservice ON service.service_id = accountservice.service_id
ORDER BY service.service_id, accountservice.fromdate 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top