I have a query which returns a record from a contract items table. At the moment it returns (correctly) the record which has the highest number in the column RECORDER:
SELECT f.ITEMNO
,f.CONTNO
,f.STATUS
,f.HIREDATE
,f.DOCDATE#5
,f.OHREC
,f.RECORDER
FROM (
SELECT ITEMNO
,MAX(RECORDER) RECORDER
FROM [LON1-ONE-APP03\INSPHIRE].[IHDATA2].[DBO].[CONTITEMS]
GROUP BY ITEMNO
) AS x
INNER JOIN [LON1-ONE-APP03\INSPHIRE].[IHDATA2].[DBO].[CONTITEMS] f ON f.ITEMNO = x.ITEMNO
AND f.RECORDER = x.RECORDER
WHERE f.ITEMNO = '01BK0039'
However, I want to change this logic so that it is as follows:
If the record status is 1, use that line and return the related information from that record.
If the record status <> 1 then use the RECORDER column and return the related information from the record with the highest number in that RECORDER column.
Only one record should ever be returned for a particular ITEMNO
In the example below, I want it to return the record with the RECORDER of 581043 as that item has a record status of 1:
+----------+------------+--------+-------------------------+-------------------------+-------+----------+
| ITEMNO | CONTNO | STATUS | HIREDATE | DOCDATE#5 | OHREC | RECORDER |
+----------+------------+--------+-------------------------+-------------------------+-------+----------+
| 01BK0039 | 00WA002946 | 1 | 2015-07-24 00:00:00.000 | 1899-12-30 00:00:00.000 | | 581043 |
| 01BK0039 | 00WA002319 | 2 | 2015-03-02 00:00:00.000 | 2015-06-30 00:00:00.000 | | 693603 |
| 01BK0039 | 00WA002712 | 2 | 2015-03-15 00:00:00.000 | 2015-03-30 00:00:00.000 | | 693688 |
+----------+------------+--------+-------------------------+-------------------------+-------+----------+
What's the best way to achieve this, seems like I need a CASE somewhere, but I can't figure out how to just return the 1 record each time.
Any help with this would be appreciated.