I have a table that holds allocations of problem reports (PRs) as follows:
TABLE "ALLOCATIONS"
ALLOCATIONID PRID DATEALLOCATED ENG_ID
1 401 20-SEP-06 10.48.00 1
2 401 20-SEP-06 10.48.00 2
3 401 20-SEP-06 10.48.00 2
4 402 20-SEP-06 12.35.00 1
5 402 20-SEP-06 12.43.00 1
6 402 20-SEP-06 13.43.00 2
7 700 14-OCT-12 13.30.05 1
8 700 14-OCT-12 13.30.35 2
9 700 14-OCT-12 14.30.35 2
The most recent allocation determines which engineer the PR is now assigned to. I want to find all the PRs that are assigned to engineer 2 for example.
So I look for the most recent allocation for each PRID, check the ENG_ID, then pull out the information from this table if the ENG_ID is correct.
This table contains the actual PR descriptions (and other info omitted here for clarity).
TABLE "PROBLEMS"
PRID TITLE
401 Something
402 Something
700 Something
To do this I have used the DATEALLOCATED field as follows:
SELECT PRID, TITLE FROM PROBLEMS p WHERE p.PRID IN
(
SELECT GROUPEDALLOC.PRID FROM allocations alloc INNER JOIN
(
SELECT PRID, MAX(DATEALLOCATED) AS MaxAllocationDate
FROM allocations
GROUP BY PRID
)
groupedAlloc ON alloc.PRID = groupedAlloc.PRID
AND ALLOC.DATEALLOCATED = groupedAlloc.MaxAllocationDate
AND ENG_ID = 2
)
ORDER BY PRID DESC;
Now this works fine for records 7,8,9 above which were inserted with a long date format that includes the seconds, however for the older records which didn’t log the seconds this will obviously not work. For these records I want to fall back on the allocationID (which may or may not be sequential obviously – however it is a last resort and better than nothing).
My question is, how do I modify my query to perform this extra condition on the DATEALLOCATED (i just want to see if they are all equal for a particular PRID), and then use the ALLOCATIONID instead?
I am using OracleXE but I want to stick to standard SQL if possible.
Does this do it for you ?