访问 - 根据字段值选择不同

时间:2016-10-06 19:45:51

标签: vba ms-access distinct distinct-on

我的代码

我有一个Access查询:

SELECT DISTINCT [Request-Priority, ALL_Q].RequestID, "Awaiting Approval" AS RequestStatus
FROM [Request-Priority, ALL_Q] LEFT JOIN LIST_RequestBurndown_Q ON [Request-Priority, ALL_Q].RequestID = LIST_RequestBurndown_Q.RequestID
WHERE (((LIST_RequestBurndown_Q.MilestoneStatus)="Awaiting Approval") AND (([Request-Priority, ALL_Q].[Completed on]) Is Null))

UNION

SELECT DISTINCT [Request-Priority, ALL_Q].RequestID, "Blocked (see notes)" AS RequestStatus
FROM [Request-Priority, ALL_Q] LEFT JOIN LIST_RequestBurndown_Q ON [Request-Priority, ALL_Q].RequestID = LIST_RequestBurndown_Q.RequestID
WHERE (((LIST_RequestBurndown_Q.MilestoneStatus)="Blocked (see notes)") AND (([Request-Priority, ALL_Q].[Completed on]) Is Null))

UNION

SELECT DISTINCT [Request-Priority, ALL_Q].RequestID, "Completed - Awaiting Rollout" AS RequestStatus
FROM [Request-Priority, ALL_Q] LEFT JOIN LIST_RequestBurndown_Q ON [Request-Priority, ALL_Q].RequestID = LIST_RequestBurndown_Q.RequestID
WHERE (((LIST_RequestBurndown_Q.MilestoneStatus)="Completed") AND (([Request-Priority, ALL_Q].[Completed on]) Is Null));

查询的基本概念......基于各个里程碑的状态(LIST_RequestBurndown_Q.MilestoneStatus),确定RequestID的总体状态([Request-Priority, ALL_Q].RequestID)。

我的问题

对于某些RequestID,会有多个状态的里程碑。例如,我可能得到如下结果:

RequestID   RequestStatus 
123         Awaiting Approval 
243         Blocked 
243         Awaiting Approval 
243         Completed - Awaiting Rollout 
542         Awaiting Approval

我想做的是"优先考虑"其中显示RequestStatus,因此每RequestID只有一条记录。

我一直在尝试使用ORDER BYWHEREDISTINCT ON来考虑如何执行此操作,或者甚至可能只创建一个包含可能状态选项的表格。相关的优先级并使用MAX - 但此时我只是过度思考这一点。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

建议:用于对优先级进行建模的表格:

CREATE TABLE RequestStatusPrioritization
( RequestStatus VARCHAR(20) NOT NULL
     REFERENCES RequestStatuses ( RequestStatus ),
  RequestStatusRank INT NOT NULL );

...然后加入此表并返回最低(MIN)排名。