我正在尝试创建一个Access查询来计算不包含字符(V)的所有不同事务。我的数据如下:
Transaction Number
997
997
998
998
998
999
999(V)
我想得到的最终结果是“2”(997和998),所以我可以消除2条记录(999和999(V))。我在考虑计算所有行,然后减去包含(V)的所有行,但不能选择(V)。这是我的查询,它一直给我0而不是1:
SELECT COUNT(*) AS C
FROM (SELECT DISTINCT [Transaction Number]
FROM [Product History]
WHERE ([Transaction Date] = #7/6/2011#) AND ([Transaction Number] LIKE '*V*')) T
感谢任何帮助!
由于
答案 0 :(得分:1)
有几种方法可以做到这一点。我选择使用LEFT JOIN / IS NULL
您也可以NOT IN
或NOT EXISTS
关键是加入ph1.[Transaction Number] & '(V)' = ph2.[Transaction Number]
SELECT COUNT ([Transaction Number]) as C
FROM (SELECT DISTINCT ph1.[Transaction Number]
FROM [Product History] ph1
LEFT JOIN [Product History] ph2
ON ph1.[Transaction Number] & '(V)' = ph2.[Transaction Number]
WHERE
ph1.[Transaction Date] = #7/6/2011#
and ph1.[Transaction Number] NOT LIKE '%V%'
and ph2.[Transaction Number] is null) t;
输出
C
-----------
2