MS Access查询确保高级零售匹配

时间:2019-03-01 19:35:44

标签: database ms-access formula

我正在尝试在MS Access中构建查询,但在寻找最佳的构建方式时遇到了问题。我要尝试做的是确保所有高级零售在一组匹配的包装编号内匹配。例如:

PackNum  Prefix    Retail
6451618  DF        37.99
6451618  SK        37.99
6451618  VJ        34.99
6451618  SG        37.99

该组中的一个已关闭,我希望查询显示它。 我正在尝试使用类似的方法进行检查,但没有得到想要的结果

 IIf([dbo_PIC704Current]![PackNum]=[dbo_PIC704Current]![PackNum] 
 And [dbo_PIC704Current]![Ret2]<>[dbo_PIC704Current]![Ret2],True,False)

任何帮助或朝正确方向推进将不胜感激! -德克

2 个答案:

答案 0 :(得分:1)

 SELECT dbo_pic704current.packnum
, Min(dbo_pic704current.Ret2) AS LowRet2
, Max(dbo_pic704current.Ret2) AS HighRet2
, dbo_CatalogInfo.MediaId
, dbo_pic704current.DiscountReasonCode
, dbo_CatalogInfo.Brand
FROM dbo_pic704current INNER JOIN dbo_CatalogInfo ON (dbo_pic704current.year = 
dbo_CatalogInfo.mailyear) AND (dbo_pic704current.catid = dbo_CatalogInfo.catalog)
WHERE (((dbo_CatalogInfo.MediaId)='CAT Catalog'))
GROUP BY dbo_pic704current.packnum, dbo_CatalogInfo.MediaId, 
dbo_pic704current.DiscountReasonCode, dbo_CatalogInfo.Brand, dbo_pic704current.Year
HAVING (((Min(dbo_pic704current.Ret2))<>Max([Ret2])))
ORDER BY dbo_pic704current.packnum;

答案 1 :(得分:1)

您现有的使用聚合的解决方案可能会产生更好的性能,但是要提供替代方案,下面是使用相关子查询的示例:

select 
    pc.packnum, pc.ret2, ci.mediaid, pc.discountreasoncode, ci.brand
from 
    dbo_pic704current pc inner join dbo_cataloginfo ci on 
    pc.year = ci.mailyear and pc.catid = ci.catalog
where 
    ci.mediaid = 'cat catalog' and exists 
    (select 1 from dbo_pic704current t where t.packnum = pc.packnum and t.ret2 > pc.ret2)
order by 
    pc.packnum