Access Query to count transactions not like
I am tryin开发者_如何学JAVAg to create an Access query to count all the distinct transactions that don´t contain the characters (V). My data looks like this:
Transaction Number
997
997
998
998
998
999
999(V)
The final result I would like to get is "2" (997 and 998), so I can eliminate the 2 records ( 999 and 999(V) ). I was thinking in counting all rows and then substract all rows that contain (V), but can´t select the (V)´s. This is the query I have and It gives me 0 all the time and not 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
Any help is appreciated!
Thanks
There are a couple of ways to do this. I chose to use LEFT JOIN / IS NULL
you could also do NOT IN
or NOT EXISTS
the key is joining 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;
which outputs
C
-----------
2
精彩评论