简体   繁体   English

MS Access查询以使用条件筛选重复项

[英]MS Access query to filter duplicates with criteria

I have 2 tables, 1Poolwaitingexchange and VA05(ZRAX) as shown in the link below. 我有2个表,如下面的链接所示,1Poolwaitingexchange和VA05(ZRAX)。

http://i.stack.imgur.com/FN9tM.png http://i.stack.imgur.com/FN9tM.png

I need [Vcat] from VA05(ZRAX) and the relationship between the 2 tables is from both [Material Code]->[Description1] and [Cust PO] -> [PO Number] (also show in link) 我需要VA05(ZRAX)中的[Vcat],两个表之间的关系来自[材料代码]-> [描述1]和[客户PO]-> [PO编号](也在链接中显示)

The only issue is, for every [Material Code] & [Cust PO], there might be duplicates with different [Vcat] meaning all the fields are the same except for [Vcat] and I want the record with the lowest Vcat but not zero. 唯一的问题是,对于每个[物料代码]和[客户采购订单],可能会有重复的[Vcat]不同,这意味着除[Vcat]以外的所有字段都相同,并且我希望记录的Vcat最低但不为零。

Eg. 例如。 If the duplicate records have [Vcat] 0, 1, 2, 3, I want to choose the record that shows [Vcat] as 1. 如果重复的记录具有[Vcat] 0、1、2、3,我想选择显示[Vcat]为1的记录。

If the records only have [Vcat] 0, I want it to show the record with [Vcat] as 0 如果记录仅具有[Vcat] 0,我希望它显示[Vcat]为0的记录

and If [Vcat] is blank, return the record with [Vcat] blank. 如果[Vcat]为空白,则返回[Vcat]为空白的记录。

The only unique record is the [Notif#] from table 1 and I have tried a lot of different approaches such as group by [Notif#], first for every duplicate field and MIN for [Vcat] but i just can't get the values i want. 唯一的唯一记录是表1中的[Notif#],我尝试了许多不同的方法,例如按[Notif#]分组,首先是每个重复字段,而[Vcat]则是MIN,但我只是无法获得我想要的价值观。

My SQL knowledge is non-existant and I do a lot of trial and error I hope somebody can help me with this issue. 我的SQL知识不存在,我做了很多试验和错误,希望有人可以帮助我解决这个问题。

You have three cases which you can UNION together. 您有三种情况可以一起使用UNION Someone would be able no doubt to come up with a single SELECT that does all three with IFF s and OR s but you might find the UNION approach easier to follow, especially if you don't know SQL. 毫无疑问,有人会提出一个使用IFFOR全部三个操作的SELECT ,但是您可能会发现UNION方法更容易遵循,特别是在您不了解SQL的情况下。

I've changed some names for my own convenience and I have used -999 because I avoid nulls: 为了方便起见,我更改了一些名称,并使用-999因为我避免使用null:

SELECT p.Material_Code, p.Cust_PO, 1 AS VCat
  FROM Pool p
 WHERE EXISTS ( SELECT *
                  FROM VARAX v
                 WHERE v.Material_Code = p.Material_Code
                       AND v.Cust_PO = p.Cust_PO
                       AND v.Vcat > 0 )
UNION
SELECT p.Material_Code, p.Cust_PO, 0 AS VCat
  FROM Pool p
 WHERE EXISTS ( SELECT *
                  FROM VARAX v
                 WHERE v.Material_Code = p.Material_Code
                       AND v.Cust_PO = p.Cust_PO
                       AND v.Vcat = 0 )
       AND NOT EXISTS ( SELECT *
                          FROM VARAX v
                         WHERE v.Material_Code = p.Material_Code
                               AND v.Cust_PO = p.Cust_PO
                               AND v.Vcat > 0 )
UNION
SELECT p.Material_Code, p.Cust_PO, -999 AS VCat
  FROM Pool p
 WHERE NOT EXISTS ( SELECT *
                    FROM VARAX v
                   WHERE v.Material_Code = p.Material_Code
                         AND v.Cust_PO = p.Cust_PO );

Sounds to me like you want something like this. 在我看来,您想要这样的事情。 Unfortunately I don't have the ability to test my Access syntax at the moment. 不幸的是,我目前无法测试我的Access语法。 (I'm also assuming that "blank" is actually null. Please clarify the datatype of Vcat if not.) (我还假定“空白”实际上为空。如果不是,请说明Vcat的数据类型。)

select
    pwe.[Cust PO], pwe.[Part Number],
    switch(
        not isnull(min(switch(Vcat between 1 and 3, Vcat))),
          min(switch(Vcat between 1 and 3, Vcat))
        min(Vcat) = 0, 0,  true, null
    ) as Vcat
from
   1Poolwaitingexchange as pwe inner join VA05 as va
       on va.[PO Number] = pwe.[Cust PO] and va.Description = pwe.[Part Number]
group by pwe.[Cust PO], pwe.[Part Number]

You're right that my logic was originally bad. 没错,我的逻辑本来就不好。 A minimum of zero was always going to supercede the legit values between 1 and 3. The modified version handles that by using logic that treats values outside that range as nulls and so they can be disregarded by the min aggregate. 最小值始终总是要取代1到3之间的合法值。修改后的版本通过使用将超出该范围的值视为空值的逻辑来处理该问题,因此min合计可以忽略它们。

You could get away with the slightly shorter versions below. 您可以使用下面略短的版本。 All three share a common English interpretation: "Is there a value between 1 and 3? Then return the smallest one." 这三个都具有相同的英语解释:“值在1到3之间吗?然后返回最小的一个。” I think the latter two read better but I don't know if you'll ever add other values that could break them. 我认为后两者的阅读效果更好,但我不知道您是否会添加其他可能破坏它们的值。 Since I don't know the nature of Vcat s and how they might change, I'll leave that to you. 由于我不了解Vcat的性质以及它们可能会如何变化,因此我将其留给您。

    switch(
        max(Vcat) > 0, min(switch(Vcat between 1 and 3, Vcat))
        min(Vcat) = 0, 0,
        true, null
    ) as Vcat

    switch(
        max(Vcat) between 1 and 3, min(switch(Vcat between 1 and 3, Vcat))
        min(Vcat) = 0, 0,
        true, null
    ) as Vcat

Instead of joining with VA05(ZRAX) directly, try joining on query results of the rows in VA05(ZRAX) grouped the way you want. 与其直接与VA05(ZRAX)连接,不如尝试对VA05(ZRAX)中的行的查询结果进行连接,并按所需的方式进行分组。 You can do that with two subqueries grouping for the min value with and without the zeroes. 您可以使用两个子查询来对最小值进行分组(带零和不带零)。

So you'll have another query named qry_va05zrax_toPool_min as 因此,您将有另一个名为qry_va05zrax_toPool_min的查询,

select 
[po number], description, 
vcatmin:min(vcat)
from VA05(ZRAX)
group by 
[po number],description

and qry_va05zrax_toPool_minGT0: 和qry_va05zrax_toPool_minGT0:

select 
[po number], description, 
vcatminGT0:min(vcat)
from VA05(ZRAX)
group by 
[po number],description
having vcat > 0

and a third one on both of those like: 和第三个都类似的:

select
[po number], description, vcatVal:iif(vcatminGT0 is not null, vcatminGT0,iif(vcatmin is not null, 0, null) )
from
1poolWaitingExchange p left outer join
qry_va05zrax_toPool_min m on
p.[po number] = m.[po number] and
p.description = m.description left outer join
qry_va05zrax_toPool_minGT0 z on
p.[po number] = z.[po number] and
p.description = z.description

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

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