I have a table CONTITEMS which shows the contracts that a unique item has been on. Each unique item can only be active on 1 contract at any given time. In this case, the STATUS field will be either 1, 6 or 8. Sometimes the item is not on an active contract and in this case, the STATUS will be either 2 or 5.
What I need to retrieve is the record for each unique item once only in the following way:
- If the record status is 1,6 or 8 use that record, if not
- select the record with the most recent DOCDATE#5 (which is the end date)
Here is an example output for 1 unique item in the CONTITEMS table:
+--------+------------+-------------------------+-------------------------+--------+----------+
| ITEMNO | CONTNO | HIREDATE | DOCDATE#5 | STATUS | RECORDER |
+--------+------------+-------------------------+-------------------------+--------+----------+
| 8021 | 00WA000587 | 2013-11-07 00:00:00.000 | 1899-12-30 00:00:00.000 | 1 | 581496 |
| 8021 | 001-016135 | 2013-07-29 00:00:00.000 | 2013-08-02 00:00:00.000 | 2 | 624239 |
| 8021 | 001-016735 | 2013-08-21 00:00:00.000 | 2013-11-06 00:00:00.000 | 2 | 626010 |
+--------+------------+-------------------------+-------------------------+--------+----------+
In this example I need to return the first row as the status is 1. However, if all the statuses were 2, then I would need to return the one with the most recent DOCDATE#5.
Here is my current SQL statement which at present returns only the record with the highest RECORDER which is not necessarily the active one or most recent:
SELECT ct.ITEMNO
,ct.CONTNO
,ct.HIREDATE
,ct.DOCDATE#5
,ct.STATUS
,ct.RECORDER
FROM (
SELECT ci.ITEMNO
,MAX(ci.RECORDER) RECORDER
FROM CONTITEMS ci
WHERE ci.ITEMNO = '8021'
GROUP BY ci.ITEMNO
) c
INNER JOIN CONTITEMS ct ON ct.RECORDER = c.RECORDER
Here is a create table and insert script:
CREATE TABLE CONTITEMS (
ITEMNO VARCHAR(20)
,CONTNO VARCHAR(10)
,HIREDATE DATETIME
,DOCDATE#5 DATETIME
,STATUS INT
,RECORDER INT
)
INSERT INTO CONTITEMS (ITEMNO, CONTNO, HIREDATE, DOCDATE#5,STATUS, RECORDER) VALUES ('8010','001-016580','2013-08-01','2013-12-03',2,582035)
INSERT INTO CONTITEMS (ITEMNO, CONTNO, HIREDATE, DOCDATE#5,STATUS, RECORDER) VALUES ('8010','001-012882','2013-03-08','2013-07-31',2,614380)
INSERT INTO CONTITEMS (ITEMNO, CONTNO, HIREDATE, DOCDATE#5,STATUS, RECORDER) VALUES ('8021','00WA000587','2013-11-07','1899-12-30',1,581496)
INSERT INTO CONTITEMS (ITEMNO, CONTNO, HIREDATE, DOCDATE#5,STATUS, RECORDER) VALUES ('8021','001-016135','2013-07-29','2013-08-02',2,624239)
INSERT INTO CONTITEMS (ITEMNO, CONTNO, HIREDATE, DOCDATE#5,STATUS, RECORDER) VALUES ('8021','001-016735','2013-08-21','2013-11-06',2,624239)
For this data, 2 records should be returned as follows:
+--------+------------+-------------------------+-------------------------+--------+----------+
| ITEMNO | CONTNO | HIREDATE | DOCDATE#5 | STATUS | RECORDER |
+--------+------------+-------------------------+-------------------------+--------+----------+
| 8010 | 001-016580 | 2013-08-01 00:00:00.000 | 2013-12-03 00:00:00.000 | 2 | 582035 |
| 8021 | 00WA000587 | 2013-11-07 00:00:00.000 | 1899-12-30 00:00:00.000 | 1 | 581496 |
+--------+------------+-------------------------+-------------------------+--------+----------+
8010 - all records at status 2 so return the most recent
8021 - has a record at status 1 so return that
Any help would be appreciated.
Martyn