Table: Process
Columns: IDProcess (PK), ProcessName
Table: ProcessStateHistory
Columns: IDStateHistory (PK), IDProcess (FK), State, CreatedOn
SELECT *
FROM Process P
WHERE (
SELECT TOP 1 PSH.State
FROM ProcessStateHistory PSH
WHERE PSH.IDProcess = P.IDProcess
ORDER BY PSH.CreatedOn DESC
) != 'Completed'
Current code:
ISortExpression processStateHistorySorter = new SortExpression(ProcessStateHistoryFields.CreatedOn | SortOperator.Descending);
IPrefetchPath2 prefetch = new PrefetchPath2(EntityType.ProcessEntity);
prefetch.Add(ProcessEntity.PrefetchPathProcessStateHistorys, 1, null, null, processStateHistorySorter);
var bucket = new RelationPredicateBucket();
bucket.Relations.Add(ProcessStateHistoryEntity.Relations.ProcessEntityUsingIDProcess);
bucket.PredicateExpression.Add(ProcessStateHistoryFields.State != "Completed");
collection = FetchEntityColl<ProcessEntity>(bucket, prefetch);
Hello I have these two tables and I am attempting to filter the [Process]'s where the latest state inserted into [ProcessStateHistory] is not 'Completed'. I am really not sure where to go at this point. This is obviously not right because I am not doing the subquery. This is giving me any [Process] that has a [ProcessStateHistory] record without "Completed".
Any help would be greatly appreciated. Thank you!!!