简体   繁体   English

访问2010 sql以选择两个记录之一

[英]access 2010 sql to select one of two records

I am pretty sure I have to use an IIF statement but I'm not sure how to implement it in this scenario... 我很确定我必须使用IIF语句,但是我不确定在这种情况下如何实现它。

I am writing a query to check user IDs from one place against a master record set. 我正在编写一个查询,以对照主记录集从一个位置检查用户ID。 However in some cases the criteria that I am using to obtain the User ID from the master record set sometimes links to more than one User ID. 但是,在某些情况下,我用来从主记录集获取用户ID的标准有时链接到多个用户ID。 One is to a user ID of people that have been deleted and one is to a user that is active. 一种是针对已删除人员的用户ID,另一种是针对活动用户。 In the case that there is two records returned, I need my query to select the active person instead of the deleted person... 在返回两个记录的情况下,我需要查询以选择活动人员而不是已删除人员...

Here is my query so far 到目前为止,这是我的查询

INSERT INTO VExceptions ( Carrier, [Wireless Number], Period,    Carrier_UserName,     Carrier_UserID, Parsed_PersNo_Carrier, MT_UserID,     MT_Cost_Center, MT_Status, [CountOfUser ID], IsSpare )
SELECT LoadFile_VCharges.Carrier, LoadFile_VCharges.[Wireless Number],     LoadFile_VCharges.Period, LoadFile_VCharges.[User Name],     LoadFile_VCharges.[User ID], LoadFile_VCharges.[Personnel Number],     MT.UserID, MT.CostCenter, MT.Status, Count(MT.UserID) AS CountOfUserID,     IIf(InStr(1,[User Name],"SPARE")>0,1,0) AS IsSpare
FROM LoadFile_VCharges LEFT JOIN MT ON LoadFile_VCharges.[Personnel Number]     = MT.PersonnelNumber
GROUP BY LoadFile_VCharges.Carrier, LoadFile_VCharges.[Wireless Number],     LoadFile_VCharges.Period, LoadFile_VCharges.[User Name],     LoadFile_VCharges.[User ID], LoadFile_VCharges.[Personnel Number],     MT.UserID, MT.CostCenter, MT.Status, IIf(InStr(1,[User Name],"SPARE")>0,1,0)
HAVING (((LoadFile_VCharges.[User ID])="NA" Or (LoadFile_VCharges.[User ID])<>    [MT].[UserID]))
ORDER BY LoadFile_VCharges.[Wireless Number];

Aside from sometimes selecting the wrong record, this query works fine... 除了有时选择错误的记录外,此查询还可以正常工作...

Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Regards, 问候,

AG 股份公司

One strategy is to left join on two subqueries, one for the active set and one for inactive set. 一种策略是在两个子查询上保留联接,一个用于活动集,一个用于非活动集。 Then in the select clause select the first option that is not null. 然后在select子句中选择不为null的第一个选项。 You can do that with COALESC, CASE, or Nz. 您可以使用COALESC,CASE或Nz来实现。 I think Access 2010 uses Nz. 我认为Access 2010使用Nz。 What Nz(active.UserID, inactive.UserID) says is "Use active.UserID if it exists, if it's null then use inactive.UserID." Nz(active.UserID,inactive.UserID)所说的是“如果存在,则使用active.UserID,如果为null,则使用inactive.UserID。”

SELECT LoadFile_VCharges.Carrier
    , LoadFile_VCharges.[Wireless Number]
    , LoadFile_VCharges.Period
    , LoadFile_VCharges.[User Name]
    , LoadFile_VCharges.[User ID]
    , LoadFile_VCharges.[Personnel Number]
    , Nz(active.UserID, inactive.UserID)
    , Nz(active.CostCenter, inactive.CostCenter)
    , Nz(active.Status, inactive.Status)
    , Count(LoadFile_VCharges.[User ID]) AS CountOfUserID
    , IIf(InStr(1,[User Name],"SPARE")>0,1,0) AS IsSpare

FROM LoadFile_VCharges 
    LEFT JOIN (SELECT UserID, CostCenter, Status FROM MT WHERE Status = 'Active') active ON (LoadFile_VCharges.[Personnel Number] = active.PersonnelNumber) 
    LEFT JOIN (SELECT UserID, CostCenter, Status FROM MT WHERE Status = 'Deleted') inactive ON (LoadFile_VCharges.[Personnel Number] = inactive.PersonnelNumber) 


GROUP BY LoadFile_VCharges.Carrier
    , LoadFile_VCharges.[Wireless Number]
    , LoadFile_VCharges.Period
    , LoadFile_VCharges.[User Name]
    , LoadFile_VCharges.[User ID]
    , LoadFile_VCharges.[Personnel Number]
    , MT.UserID, MT.CostCenter
    , MT.Status
    , IIf(InStr(1,[User Name],"SPARE")>0,1,0)

HAVING (((LoadFile_VCharges.[User ID])="NA" Or (LoadFile_VCharges.[User ID])<>[MT].[UserID]))

ORDER BY LoadFile_VCharges.[Wireless Number];

You may need to fiddle with the syntax. 您可能需要弄弄语法。 You might only need to Nz on the UserID column, I'm not sure. 我不确定,您可能只需要在UserID列上输入Nz即可。 I'm sure there are other ways of doing it. 我敢肯定还有其他方法可以做到。 I'm trying to think of a join where you get then entire active set (full join) and only the inactive set that does NOT overlap the active set (outer join). 我试图考虑一个联接,在联接中您将获得整个活动集(完全联接),并且只有不与活动集重叠的非活动集(外部联接)。 In MS SQL (non-Access) I would use a windowing function, but I don't think that doesn't exists in Access. 在MS SQL(非Access)中,我将使用窗口功能,但我认为Access中不存在这种功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM